0

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

  • Could u share a subset of the actual json? – sammywemmy Apr 12 '20 at 00:38
  • @samnywemmy thanks, I am looking for something like this [{'name':'Bakerloo','statusSeverityDescription':'Part Suspended'}, {..}...] But to be honest anything that is much smaller than the original and contains the line and the current status would be good. Thanks a lot! – JoScratcherJo Apr 12 '20 at 09:21

1 Answers1

1

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]}]
sammywemmy
  • 27,093
  • 4
  • 17
  • 31