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.
print(data['cosponsors'][0]['thomas_id']- note 0, not "0"