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

surgically extracting datum from nested json object using python

From this nested JSON object I need to parse out and pretty print the value for

"id"
and the value for
"location"
.

{
"links": {
"self": "http://localhost:2510/api/v2/jobs?skills=data%20science"
},
"data": [
{
"id": 121,
"type": "job",
"attributes": {
"title": "Data Scientist",
"date": "2014-01-22T15:25:00.000Z",
"description": "Data scientists are in increasingly high demand amongst tech companies in London. Generally a combination of business acumen and technical skills are sought. Big data experience ..."
},
"relationships": {
"location": {
"links": {
"self": "http://localhost:2510/api/v2/jobs/121/location"
},
"data": {
"type": "location",
"id": 3
}
},
"country": {
"links": {
"self": "http://localhost:2510/api/v2/jobs/121/country"
},
"data": {
"type": "country",
"id": 1
}
},


I've been trying to grab it in this way:

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


for item in data["data"]:
for job in data['id']:
for title in data['data']:
print(title.get('location')


but I've not been able to grab the data I need.

How can I surgically extract only those datum that I'm interested in?

This is the full file.




EDIT

I've been trying this as some kind of exploration, but even this crashes before the file finishes:

import json
from pprint import pprint

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


for item in data["data"]:
for job in item:
print( job )

Answer

I'm taking a leap and guessing the info you really want in the end, which is a list of item IDs for each location ID:

import json
from collections import defaultdict

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

locations = defaultdict(list)

for item in data['data']:
    location = item['relationships']['location']['data']['id']
    locations[location].append(item['id'])

print(locations)