Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: interface conversion interface {} is []interface {}, not map[string]interface {}

Tags:

json

go

I am building a project that takes a term from the user and then performs a google search and returns a list of titles in json format.

I am using the serpwow API to perform the google search and am trying to parse the response.

However I am getting the error that states:

panic: interface conversion: interface {} is []interface {}, not map[string]interface {}.

I have looked through various forms and have tried to learn how mapping works but I am not sure why in this case, my mapping is not working. The table for organic results looks like this:

"organic_results": [
    {
      "position": 1,
      "title": "The 10 Best Pizza Places in Dublin - TripAdvisor",
      "link": "https://www.tripadvisor.ie/Restaurants-g186605-c31-Dublin_County_Dublin.html",
      "domain": "www.tripadvisor.ie",
      "displayed_link": "https://www.tripadvisor.ie › ... › County Dublin › Dublin",
      "snippet": "Best Pizza in Dublin, County Dublin: Find TripAdvisor traveller reviews of Dublin Pizza places and search by price, location, and more.",
      "prerender": false,
      "snippet_matched": [
        "Pizza",
        "Pizza"
      ],
      "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:OS-Ar9hB_ngJ:https://www.tripadvisor.ie/Restaurants-g186605-c31-Dublin_County_Dublin.html+&cd=4&hl=en&ct=clnk&gl=ie",
      "related_page_link": "https://www.google.com/search?q=related:https://www.tripadvisor.ie/Restaurants-g186605-c31-Dublin_County_Dublin.html+pizza&tbo=1&sa=X&ved=2ahUKEwicjYKvvNjmAhVoSBUIHa9MBhcQHzADegQIARAH",
      "block_position": 2
    },

and here is a snip of my code:

package main

import (
    "fmt"
    "strings"
    serpwow "github.com/serpwow/google-search-results-golang"
)

func main() {
    // set API key
    apiKey := "Key_Hidden"

    //read term to search
    fmt.Print("What term would you like to search in google? ")
    var term string
    fmt.Scanln(&term)

    // set up our search parameters
    parameters := map[string]interface{}{
        "q": term,
    }

    // retrieve the search results as JSON
    response, error := serpwow.GetJSON(parameters, apiKey)

    // print the response, or error, if one occurred
    if error != nil {
        fmt.Println(error)
    } else {
    //extract title from organic results
    //result := fmt.Sprintf("%v", response["organic_results"].(map[string]interface{})["title"])
    for _, item := range response["organic_results"].([]interface{}) {
        fmt.Sprintf("%v", item.(map[string]interface{})["title"])
    }
    //s := []string{result, "\n"}
    //fmt.Printf(strings.Join(s, " "))

}

} Can someone please help me figure our where my logic is wrong?

like image 666
Marodr Avatar asked Jan 15 '20 20:01

Marodr


1 Answers

response["organic_results"] corresponds to the JSON array "organic_results", hence it is not a map[string]interface{}, but a []interface. There are multiple results, not one.

for _,item:=range respose["organic_results"].([]interface{}) {
    fmt.Printf("%v", item.(map[string]interface{})["title"])
}
like image 81
Burak Serdar Avatar answered Sep 22 '22 00:09

Burak Serdar