Michael Perdue Michael Perdue - 7 months ago 30
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:

print(data['cosponsors']['0']['thomas_id']


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

Background:

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']
['type'])


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

Answer

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

print(data['cosponsors'][0]['thomas_id'])

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']:
    print(cosponsor['thomas_id'])

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