jsongoduckduckgo-api

Decoding JSON from the DuckDuckGo using Golang


I've been using the Go library for DuckDuckGo and it has been working fine except for one thing. In the "RelatedTopics" Section. Usually, it would look like this:

{
"RelatedTopics" : [
      {
         "Result" : "<a href=\"http://duckduckgo.com/Criticism_of_Google\">Criticism of Google</a> - Criticism of Google includes possible misuse and manipulation of search results, its use of others' intellectual property, concerns that its compilation of data may violate people's privacy, cen...",
         "Icon" : {
            "URL" : "",
            "Height" : "",
            "Width" : ""
         },
         "FirstURL" : "http://duckduckgo.com/Criticism_of_Google",
         "Text" : "Criticism of Google - Criticism of Google includes possible misuse and manipulation of search results, its use of others' intellectual property, concerns that its compilation of data may violate people's privacy, cen..."
      },
      {
         "Result" : "<a href=\"http://duckduckgo.com/PRISM_(surveillance_program)\">PRISM</a> - PRISM is a clandestine mass electronic surveillance data mining program known to have been operated by the United States National Security Agency (NSA) since 2007.",
         "Icon" : {
            "URL" : "",
            "Height" : "",
            "Width" : ""
         },
         "FirstURL" : "http://duckduckgo.com/PRISM_(surveillance_program)",
         "Text" : "PRISM - PRISM is a clandestine mass electronic surveillance data mining program known to have been operated by the United States National Security Agency (NSA) since 2007."
      },
      {
         "Result" : "<a href=\"http://duckduckgo.com/Google_Panda\">Google Panda</a> - Google Panda is a change to Google's search results ranking algorithm that was first released in February 2011.",
         "Icon" : {
            "URL" : "",
            "Height" : "",
            "Width" : ""
         },
         "FirstURL" : "http://duckduckgo.com/Google_Panda",
         "Text" : "Google Panda - Google Panda is a change to Google's search results ranking algorithm that was first released in February 2011."
      },
      {
         "Result" : "<a href=\"http://duckduckgo.com/Google_Penguin\">Google Penguin</a> - Google Penguin is a code name for a Google algorithm update that was first announced on April 24, 2012.",
         "Icon" : {
            "URL" : "",
            "Height" : "",
            "Width" : ""
         },
         "FirstURL" : "http://duckduckgo.com/Google_Penguin",
         "Text" : "Google Penguin - Google Penguin is a code name for a Google algorithm update that was first announced on April 24, 2012."
      },
      {
         "Result" : "<a href=\"http://duckduckgo.com/Google_search_features\">Google search features</a> - The Google search features include more than 40 options or keywords to modify the type of search.",
         "Icon" : {
            "URL" : "",
            "Height" : "",
            "Width" : ""
         },
         "FirstURL" : "http://duckduckgo.com/Google_search_features",
         "Text" : "Google search features - The Google search features include more than 40 options or keywords to modify the type of search."
      },
      {
         "Result" : "<a href=\"http://duckduckgo.com/Google_Searchology\">Google Searchology</a> - Google Searchology 2009 witnessed several novel changes in the Google search options.",
         "Icon" : {
            "URL" : "",
            "Height" : "",
            "Width" : ""
         },
         "FirstURL" : "http://duckduckgo.com/Google_Searchology",
         "Text" : "Google Searchology - Google Searchology 2009 witnessed several novel changes in the Google search options."
      },
      {
         "Result" : "<a href=\"http://duckduckgo.com/c/Multilingual_websites\">Multilingual websites</a>",
         "Icon" : {
            "URL" : "",
            "Height" : "",
            "Width" : ""
         },
         "FirstURL" : "http://duckduckgo.com/c/Multilingual_websites",
         "Text" : "Multilingual websites"
      },
      {
         "Result" : "<a href=\"http://duckduckgo.com/c/Google_services\">Google services</a>",
         "Icon" : {
            "URL" : "",
            "Height" : "",
            "Width" : ""
         },
         "FirstURL" : "http://duckduckgo.com/c/Google_services",
         "Text" : "Google services"
      },
      {
         "Result" : "<a href=\"http://duckduckgo.com/c/Internet_search_engines\">Internet search engines</a>",
         "Icon" : {
            "URL" : "",
            "Height" : "",
            "Width" : ""
         },
         "FirstURL" : "http://duckduckgo.com/c/Internet_search_engines",
         "Text" : "Internet search engines"
      },
      {
         "Result" : "<a href=\"http://duckduckgo.com/c/American_websites\">American websites</a>",
         "Icon" : {
            "URL" : "",
            "Height" : "",
            "Width" : ""
         },
         "FirstURL" : "http://duckduckgo.com/c/American_websites",
         "Text" : "American websites"
      }
   ]
}

However, sometimes in the "RealtedTopics" section, series of "Topics" with numerous "Results" in them and a "Name" of that Topic appear.

I apologize if that didn't make much a sense. A good example of this is when you search Doctor Who.

{
"RelatedTopics" : [
      {
         "Result" : "<a href=\"http://duckduckgo.com/Doctor_Who\">Doctor Who</a> is the title of a long-running British science fiction series.",
         "Icon" : {
            "URL" : "https://i.duckduckgo.com/i/www.bbc.co.uk.ico",
            "Height" : 16,
            "Width" : 16
         },
         "FirstURL" : "http://duckduckgo.com/Doctor_Who",
         "Text" : "Doctor Who is the title of a long-running British science fiction series."
      },
      {
         "Result" : "<a href=\"http://duckduckgo.com/Doctor_(Doctor_Who)\">Doctor (Doctor Who)</a>, the main character in the British television series",
         "Icon" : {
            "URL" : "https://i.duckduckgo.com/i/e32c2703.jpg",
            "Height" : "",
            "Width" : ""
         },
         "FirstURL" : "http://duckduckgo.com/Doctor_(Doctor_Who)",
         "Text" : "Doctor (Doctor Who), the main character in the British television series"
      },
      {
         "Result" : "<a href=\"http://duckduckgo.com/Doctor_Who_theme_music\">Doctor Who theme music</a>, the theme music created for the television series, and has changed many times over the 50 years it has been running.",
         "Icon" : {
            "URL" : "",
            "Height" : "",
            "Width" : ""
         },
         "FirstURL" : "http://duckduckgo.com/Doctor_Who_theme_music",
         "Text" : "Doctor Who theme music, the theme music created for the television series, and has changed many times over the 50 years it has been running."
      },
      {
         "Topics" : [
            {
               "Result" : "<a href=\"http://duckduckgo.com/Doctor_Who_(film)\">Doctor Who (film)</a>, the television movie starring Paul McGann, based on the television series",
               "Icon" : {
                  "URL" : "",
                  "Height" : "",
                  "Width" : ""
               },
               "FirstURL" : "http://duckduckgo.com/Doctor_Who_(film)",
               "Text" : "Doctor Who (film), the television movie starring Paul McGann, based on the television series"
            },
            {
               "Result" : "<a href=\"http://duckduckgo.com/Dr._Who_(Dalek_films)\">Dr. Who (Dalek films)</a>, the human character played by Peter Cushing in two films based on the television series",
               "Icon" : {
                  "URL" : "https://i.duckduckgo.com/i/9f10647e.jpg",
                  "Height" : "",
                  "Width" : ""
               },
               "FirstURL" : "http://duckduckgo.com/Dr._Who_(Dalek_films)",
               "Text" : "Dr. Who (Dalek films), the human character played by Peter Cushing in two films based on the television series"
            },
            {
               "Result" : "<a href=\"http://duckduckgo.com/Doctor_Who_(pinball)\">Doctor Who (pinball)</a>, pinball machine based on the television series",
               "Icon" : {
                  "URL" : "https://i.duckduckgo.com/i/eee6de34.jpg",
                  "Height" : "",
                  "Width" : ""
               },
               "FirstURL" : "http://duckduckgo.com/Doctor_Who_(pinball)",
               "Text" : "Doctor Who (pinball), pinball machine based on the television series"
            },
            {
               "Result" : "<a href=\"http://duckduckgo.com/Doctor_Who_theme_music\">Doctor Who theme music</a>, the theme music created for the television series, and has changed many times over the 50 years it has been running.",
               "Icon" : {
                  "URL" : "",
                  "Height" : "",
                  "Width" : ""
               },
               "FirstURL" : "http://duckduckgo.com/Doctor_Who_theme_music",
               "Text" : "Doctor Who theme music, the theme music created for the television series, and has changed many times over the 50 years it has been running."
            },
            {
               "Result" : "<a href=\"http://duckduckgo.com/Doctor_Who_(CSI)\">\"Doctor Who\" (CSI)</a>, the 22nd episode of the 10th season of CSI: Crime Scene Investigation",
               "Icon" : {
                  "URL" : "",
                  "Height" : "",
                  "Width" : ""
               },
               "FirstURL" : "http://duckduckgo.com/Doctor_Who_(CSI)",
               "Text" : "\"Doctor Who\" (CSI), the 22nd episode of the 10th season of CSI: Crime Scene Investigation"
            }
         ],
         "Name" : "In media and entertainment"
      },
      {
         "Topics" : [
            {
               "Result" : "<a href=\"http://duckduckgo.com/Neoregelia_'Dr._Who'\">Neoregelia 'Dr. Who'</a>, a hybrid cultivar of the genus Neoregelia in the Bromeliad family",
               "Icon" : {
                  "URL" : "",
                  "Height" : "",
                  "Width" : ""
               },
               "FirstURL" : "http://duckduckgo.com/Neoregelia_'Dr._Who'",
               "Text" : "Neoregelia 'Dr. Who', a hybrid cultivar of the genus Neoregelia in the Bromeliad family"
            }
         ],
         "Name" : "In other uses"
      }
   ]
}

How would I extend the library so that I could access these "Topics"? I cannot think of way using json.Unmarshal (this what the library uses) to do it.

Regards,

Ginger Bill


Solution

  • It seem like the Topic includes within it a slice Topics []Topic:

    type Icon struct {
      URL string
      Height string
      Width string
    }
    
    type Topic struct {
      Result string
      Icon Icon
      FirstURL string
      Text string
      Topics []Topic `json:",omitempty"`
    }
    
    type RootObj struct {
     RelatedTopics []Topic
    }