1

I'm using this python script (in an attempt) to extract a nested list from a 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 ]?

1
  • I've removed this link from the question, as the content within has been deleted. Please try to add examples directly to questions, to avoid external link breakage. Commented Jan 23, 2018 at 10:34

4 Answers 4

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

how would it be possible to output the name too?
You would use the same pattern - record.get('attributes', {}).get('name', None)
1

You have to access the sub-structure with its key:

coords = {}
for item in data['included']:
    coords[item['id']] = item['attributes']['coords']

Comments

0
>>> data
{'id': 3, 'attributes': {'coord': [51.503378, -0.139134], 'name': 'Victoria'}, 'type': 'location'}
>>> from operator import itemgetter
>>> my_id = itemgetter('id')
>>> attributes = itemgetter('attributes')
>>> coord = itemgetter('coord')
>>> 
>>> my_id(data), coord(attributes(data))
(3, [51.503378, -0.139134])
>>> {my_id(data) : coord(attributes(data))}
{3: [51.503378, -0.139134]}
>>> d = {}
>>> d[my_id(data)] = coord(attributes(data))
>>> d
{3: [51.503378, -0.139134]}
>>> 

Comments

0

I am assuming, the id and type are always provided through JSON response, and if type is location then coord will be given too:

location_map = {}

for item in data.get('included', [])
    if item['type'] == 'location':
        location_map[item['id']] = item['attributes']['coord']

print location_map

OR in more pythonic way:

location_map = {
    item['id']: item['attributes']['coord']
    for item in data.get('included', []) if item['type'] == 'location'
}
print location_map

For sample input:

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

result would be:

{3: [51.503378, -0.139134]}

For reference see Dict Comprehensions: https://www.python.org/dev/peps/pep-0274/

4 Comments

:/ I don't understand how that would fit in the code.
I've seen the JSON response coord will only be present on the items that are having type equals to 'location'. So, you just have to replace your location with what I've provided (i.e. location_map). I don't think there is any rocket science in this. for reference see dict comprehensions: python.org/dev/peps/pep-0274
@s.matthew.english did you get it?
@s.matthew.english, updated my answer with another way of achieving the same outcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.