Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I loop over entries in JSON?

Tags:

python

json

I want to loop over the content of a JSON file and print it to the console.

I think I did mix up something with lists.

This is what I tried to get all the team_name elements

from urllib2 import urlopen import json  url = 'http://openligadb-json.heroku.com/api/teams_by_league_saison?league_saison=2012&league_shortcut=bl1' response = urlopen(url) json_obj = json.load(response)  for i in json_obj['team']:     print i 

And this is my JSON (simplified:)

{     "team": [         {             "team_icon_url": "http://www.openligadb.de/images/teamicons/Hamburger_SV.gif",             "team_id": "100",             "team_name": "Hamburger SV"         },         {             "team_icon_url": "http://www.openligadb.de/images/teamicons/FC_Schalke_04.gif",             "team_id": "9",             "team_name": "FC Schalke 04"         }     ] } 

(Full JSON output to be found here: Link)

And of course I get an error, that I should use integer input in [], not string, but I don't get how I could do that.

for i in json_obj['team']: TypeError: string indices must be integers, not str 

Here is the response:

http://openligadb-json.heroku.com/api/teams_by_league_saison?league_saison=2012&league_shortcut=bl1 <addinfourl at 139755086292608 whose fp = <socket._fileobject object at 0x7f1b446d33d0>> 

What did I get wrong?

like image 610
mcbetz Avatar asked Jan 27 '13 13:01

mcbetz


People also ask

Can you loop through JSON?

To loop through a JSON array with JavaScript, we can use a for of loop. to loop through the json array with a for of loop. We assign the entry being looped through to obj . Then we get the value of the id property of the object in the loop and log it.

How do you loop a JSON response?

Use Object. values() or Object. entries(). These will return an array which we can then iterate over. Note that the const [key, value] = entry; syntax is an example of array destructuring that was introduced to the language in ES2015.

How do I iterate over an array in JSON?

1) Create a Maven project and add json dependency in POM. xml file. 2) Create a string of JSON data which we convert into JSON object to manipulate its data. 3) After that, we get the JSON Array from the JSON Object using getJSONArray() method and store it into a variable of type JSONArray.

How do I iterate over a JSON object?

getJSONObject("JObjects"); JSONArray getArray = getObject. getJSONArray("JArray1"); for(int i = 0; i < getArray. size(); i++) { JSONObject objects = getArray. getJSONArray(i); //Iterate through the elements of the array i. //Get thier value. //Get the value for the first element and the value for the last element. }


Video Answer


2 Answers

Actually, to query the team_name, just add it in brackets to the last line. Apart from that, it seems to work on Python 2.7.3 on command line.

from urllib2 import urlopen import json  url = 'http://openligadb-json.heroku.com/api/teams_by_league_saison?league_saison=2012&league_shortcut=bl1' response = urlopen(url) json_obj = json.load(response)  for i in json_obj['team']:     print i['team_name'] 
like image 73
mcbetz Avatar answered Oct 08 '22 23:10

mcbetz


Try this :

import urllib, urllib2, json url = 'http://openligadb-json.heroku.com/api/teams_by_league_saison?league_saison=2012&league_shortcut=bl1' request = urllib2.Request(url) request.add_header('User-Agent','Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)') request.add_header('Content-Type','application/json') response = urllib2.urlopen(request) json_object = json.load(response) #print json_object['results'] if json_object['team'] == []:     print 'No Data!' else:     for rows in json_object['team']:         print 'Team ID:' + rows['team_id']         print 'Team Name:' + rows['team_name']         print 'Team URL:' + rows['team_icon_url'] 
like image 40
Prem Minister Avatar answered Oct 08 '22 23:10

Prem Minister