s.matthew.english s.matthew.english - 10 months ago 60
JSON Question

sorting output of python file by location. increment by common id

There's multiple instances of data that share the same location id's, for example in the output below there are many


121 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/121/location'}}
122 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/122/location'}}
120 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/120/location'}}
119 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/119/location'}}
191 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/191/location'}}
190 {'data': {'id': 52, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/190/location'}}
193 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/193/location'}}
187 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/187/location'}}
189 {'data': {'id': 52, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/189/location'}}
186 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/186/location'}}
198 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/198/location'}}
196 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/196/location'}}
199 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/199/location'}}
201 {'data': {'id': 3, 'type': 'location'}, 'links': {'self': 'http://localhost:2510/api/v2/jobs/201/location'}}

I'd like to sort these all in the style of:

{'data': {'id': 3, 'type': 'location'} 15
{'data': {'id': 4, 'type': 'location'} 6
{'data': {'id': 5, 'type': 'location'} 0
{'data': {'id': 6, 'type': 'location'} 11

Is there a way to adapt that python script to output the data like that?

Actually it's coming from this JSON file, that looks like so:

"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
"skills": {
"links": {

and parsed using the following python script:

import json
from pprint import pprint

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

for item in data["data"]:
print(item['id'], item['relationships']['location'])

This is the full data file in my GitHub.

uav uav
Answer Source

Put data in database (SQLite for example) then "GROUP BY".