Michael Perdue Michael Perdue - 2 years ago 238
Python Question

print json object nested in an array using python

I am working with a JSON file and am using Python. I am trying to print an object that is nested in an array. I would like to print select objects (e.g. "name", "thomas_id") from the following array (is it considered a 'list' of 'objects' in an array? would the array be called the "cosponsors" array?):

"cosponsors": [
"district": null,
"name": "Akaka, Daniel K.",
"sponsored_at": "2011-01-25",
"state": "HI",
"thomas_id": "00007",
"title": "Sen",
"withdrawn_at": null
"district": null,
"name": "Lautenberg, Frank R.",
"sponsored_at": "2011-01-25",
"state": "NJ",
"thomas_id": "01381",
"title": "Sen",
"withdrawn_at": null

The problem is that I do not know the syntax to print objects (listed?) in an array. I have tried a number of variations extrapolated from what I have found on stack overflow; namely, variations of the following:


I recieve the error "list indices must be integers or slices, not str"


I have over 3000 json files that are contained within a so-called master file. I only need the same specific aspects of each file that I will need to later export into a MYSQL DB, but that is another topic (or is it, i.e. am I going about this the wrong way?). Accordingly, I am writing a code that I can impliment on all of the files in order to get the data that I need. I've been doing quite well, considering that I do not have any experience with programming. I have been using the following code in Python:

import json

data = json.load(open('s2_data.json', 'r'))

print (data["official_title"], data["number"], data["introduced_at"],
data["bill_id"], data['subjects_top_term'], data['subjects'],
data['summary']['text'], data['sponsor']['thomas_id'],
data['sponsor']['state'], data['sponsor']['name'], data['sponsor']

It has been returning results that are separated with a space. So far I am happy with that.

Answer Source

You are using a string to index the list, '0' is a string, not an integer. Remove the quotes:


When in doubt, check the partial result; see what print(type(data['cosponsors'])) produces; if that produces <type 'list'>, you know you need to use indexing with integers, if you get <type 'dict'>, use keys (a list of which can be gotten by calling print(list(...)) on the dictionary), etc.

Usually, lists contain a variable number of objects; it could be just one, zero or a whole load more. You could loop over those objects:

for cosponsor in data['cosponsors']:

The loop sets cosponsor to each of the objects in the data['cosponsors'] list, one by one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download