Search code examples
pythonjsoncsvexport-to-csvreadfile

getting JSON data from python several levels deep without names


So I've written some simple python code to do some web-scraping and I'm fairly noob, so i have a question. I get my json data using:

results = response.json()  

this causes me no problems with my site entered and parameters correct.

This JSON file has a few different groups, one of which is entitled 'moments', which itself goes fairly deep.

So, for example to get part of what i want, i can do a

print results['moments'][0][5]

but what i really want is to get

results['moments'][0][5]
results['moments'][1][5]
results['moments'][2][5]
results['moments'][3][5]

etc... through several hundred, so i'm not sure how to iterate that and keep the [5] at the next tier.

The reason i don't just use the full output of results['moments'] is i want to export this to excel, and if i just write using csv_writer on just

results['moments']

it doesn't actually comma seperate the values so i end up with long bracketed values within Column 1, but if i go to the 3rd level it will be comma seperated when i output to excel.

i'm sure there are several ways to resolve this issue. see code below

response = session.get('http://xxxxxxxxxxxx', params=params)
     
results = response.json()

location = results['moments'][0][5]    
print location


with open('Location1.csv', 'wb') as test_file:
    csv_writer = csv.writer(test_file)
    for y in location:
        csv_writer.writerow(y) 

Solution

  • Instead of doing

    results['moments'][0][5]
    results['moments'][1][5]
    results['moments'][2][5]
    results['moments'][3][5]
    

    You can use a simple list comprehension to do this for you, where you iterate on the length of the list results['moments']. Note that the other index remains fixed, as shown below:

    locations = [results['moments'][i][5] for i in xrange(len(results['moments']))]
    

    or

    locations = [moment[5] for moment in results['moments']]