Snippets

stíobhart matulevicz Nested JSON Parsing

Created by stíobhart matulevicz
/* Here is an example of the JSON I'm trying to parse [It's from the Guardian website]:

{"response":{"status":"ok","userTier":"developer","total":19,"startIndex":1,"pageSize":1,"currentPage":1,"pages":19,"orderBy":"relevance","results":[{"type":"article","sectionId":"technology","webTitle":"Apple removes adblockers that work on Facebook and other third-party apps","webPublicationDate":"2015-10-09T10:02:09Z","id":"technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps","webUrl":"http://www.theguardian.com/technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps","apiUrl":"http://content.guardianapis.com/technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps","sectionName":"Technology"}]}}

I want to get "webTitle" which is nested inside "results" which is nested inside "response"

*/

// get the JSON from URL

response, err := //http.Get("http://content.guardianapis.com/search?from-date=2015-10-03&q=iphone&api-key=zksex4jr82dg2gkcqtyq5xp8")
		if err != nil {
			fmt.Printf("%s", err)
			os.Exit(1)
		} else {
			defer response.Body.Close()
			contents, err := ioutil.ReadAll(response.Body)
			if err != nil {
				fmt.Printf("%s", err)
				os.Exit(1)
			}

//unmarshal JSON into "parse1"
var parse1 map[string]interface{}
if err := json.Unmarshal(contents, &parse1); err != nil {
				panic(err)
			}
fmt.Println("||||||PARSE1: ", (reflect.TypeOf(parse1)), " ||||||\n", parse1, "|||||||| END PARSE1")

/* Produces:

||||||PARSE1 which is  map[string]interface {}  ||||||
 map[response:map[status:ok userTier:developer total:19 startIndex:1 currentPage:1 pages:19 orderBy:relevance pageSize:1 results:[map[webTitle:A
pple removes adblockers that work on Facebook and other third-party apps webPublicationDate:2015-10-09T10:02:09Z id:technology/2015/oct/09/apple
-removes-iphone-adblockers-facebook-third-party-apps webUrl:http://www.theguardian.com/technology/2015/oct/09/apple-removes-iphone-adblockers-fa
cebook-third-party-apps apiUrl:http://content.guardianapis.com/technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps
sectionName:Technology type:article sectionId:technology]]]] |||||||| END PARSE1

OK. Now lets get the "response" map:
*/

parse2 := parse1["response"]
fmt.Println("||||||PARSE2: ", (reflect.TypeOf(parse2)), " ||||||\n", parse2, "|||||||| END PARSE2")

/* Produces:

||||||PARSE2 which is  map[string]interface {}  ||||||
 map[pageSize:1 results:[map[sectionName:Technology type:article sectionId:technology webTitle:Apple removes adblockers that work on Facebook an
d other third-party apps webPublicationDate:2015-10-09T10:02:09Z id:technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-
apps webUrl:http://www.theguardian.com/technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps apiUrl:http://content.gu
ardianapis.com/technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps]] total:19 startIndex:1 currentPage:1 pages:19 o
rderBy:relevance status:ok userTier:developer] |||||||| END PARSE2

Now get the "results" map:
*/

parse3 := parse2["results"]
fmt.Println("||||||PARSE3: ", (reflect.TypeOf(parse3)), " ||||||\n", parse3, "|||||||| END PARSE3")

/* Produces:

invalid operation: parse2["results"] (type interface {} does not support indexing)

So, why was does: 'parse2 := parse1["response"]' work but 'parse3 := parse2["results"]' doesn't?

[All assuming I'm even going aobut trying to do this the right way in the first place!]

*/

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.