简体   繁体   中英

Extract subset of key-value pairs from Python dictionary object of a specific API (TfL)

I am trying to elegantly extract a very simple dictionary from a rather complex one, more specifically, Transport for London (TfL) provides an API with the status of the underground (tube). But the response is so long that my ESP32 cant handle it. Hence I have written a python script that reads the API response and deletes unused elements of the dictiionary like so

for x in linestatus: del x['lineStatuses'][0]['$type']

However I realize this is far from pretty and I am looking for a solution similar to this: Extract subset of key-value pairs from Python dictionary object?

But I cant get my head around how to do it with this specific dictionary. Here is the dict/JSON https://api.tfl.gov.uk/line/mode/tube/status

I would like to extract a simple dictionary only with

[{'name':'Bakerloo','statusSeverityDescription':'Part Suspended'}, {..}...]

with the maximum 'statusSeverity' per line and only those disruptions where the ['validityPeriod']['isNow'] is true.

For clarification see the picture....

many thanks - trying to learn...

visual representation of the JSON/dictionary

one way to solve your challenge is to use jmespath , as your data is nested; play with it a bit, as I believe it should help with ur question:

the json data is wrapped in a variable called content

import jmespath
expression = jmespath.compile('[].{name:name,status_severity:lineStatuses[].statusSeverity, isnow:lineStatuses[].validityPeriods[].isNow}')
expression.search(content)

[{'name': 'Bakerloo', 'status_severity': [0, 3], 'isnow': [True, True]},
 {'name': 'Central', 'status_severity': [3, 0], 'isnow': [True, True]},
 {'name': 'Circle', 'status_severity': [4, 20], 'isnow': [False, True]},
 {'name': 'District', 'status_severity': [3, 0], 'isnow': [True, True]},
 {'name': 'Hammersmith & City', 'status_severity': [0], 'isnow': [True]},
 {'name': 'Jubilee', 'status_severity': [3, 0], 'isnow': [True, True]},
 {'name': 'Metropolitan', 'status_severity': [3, 0], 'isnow': [True, True]},
 {'name': 'Northern', 'status_severity': [0], 'isnow': [True]},
 {'name': 'Piccadilly', 'status_severity': [3, 0], 'isnow': [True, True]},
 {'name': 'Victoria', 'status_severity': [0], 'isnow': [True]},
 {'name': 'Waterloo & City',
  'status_severity': [4, 20],
  'isnow': [False, True]}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM