s.matthew.english s.matthew.english - 10 days ago 5
JSON Question

extract a nested list from this JSON object with python

I'm using this python script (in an attempt) to extract a nested list from this JSON object.

import json
from collections import defaultdict
from pprint import pprint

with open('data-science.txt') as data_file:
data = json.load(data_file)

locations = defaultdict(int)

for item in data['included']:
location = item['attributes']
print(location)


I get the following output:

{'name': 'Victoria', 'coord': [51.503378, -0.139134]}
{'name': 'United Kingdom', 'coord': None}
{'name': 'data science'}
{'CEO': None, 'abbreviation': None, 'logoUrl': None, 'title': 'Make IT London'}
{'name': 'Victoria', 'coord': [51.503378, -0.139134]}
{'name': 'United Kingdom', 'coord': None}
{'name': 'data science'}
{'CEO': None, 'abbreviation': None, 'logoUrl': None, 'title': 'Make IT London'}
{'name': 'Victoria', 'coord': [51.503378, -0.139134]}
{'name': 'United Kingdom', 'coord': None}
{'name': 'data science'}
{'CEO': None, 'abbreviation': None, 'logoUrl': None, 'title': 'Make IT London'}
{'name': 'Victoria', 'coord': [51.503378, -0.139134]}
{'name': 'United Kingdom', 'coord': None}
{'name': 'data science'}
{'CEO': None, 'abbreviation': None, 'logoUrl': None, 'title': 'Make IT London'}
{'name': 'Victoria', 'coord': [51.503378, -0.139134]}
{'name': 'United Kingdom', 'coord': None}
{'name': 'data mining'}
{'name': 'data analysis'}


But really what I want is the
'coord'
list associated with an
"id"
.

A single record looks like this:

{
"id": 3,
"type": "location",
"attributes": {
"name": "Victoria",
"coord": [
51.503378,
-0.139134
]
}
},


How can I extract the only the
"id": 3
and
"coord": [ 51.503378, -0.139134 ]
?

Answer

This is a little bare-bones but may help. Baseline - you might want to use the get function in python. (See this: https://docs.python.org/2/library/stdtypes.html#dict.get)

I won't expand too much on the below code - it is fairly simple - but you can add some logic around it to check if id is None or if coord is None and do additional processing for your own purposes.

for record in data['included']:
    id = record.get('id', None)
    coord = record.get('attributes', {}).get('coord', None)