-1

I'm new to python as was wondering how I could get the estimatedWait and routeName from this string.

{
  "lastUpdated": "07:52",
  "filterOut": [],
  "arrivals": [
    {
      "routeId": "B16",
      "routeName": "B16",
      "destination": "Kidbrooke",
      "estimatedWait": "due",
      "scheduledTime": "06: 53",
      "isRealTime": true,
      "isCancelled": false
    },
    {
      "routeId":"B13",
      "routeName":"B13",
      "destination":"New Eltham",
      "estimatedWait":"29 min",
      "scheduledTime":"07:38",
      "isRealTime":true,
      "isCancelled":false
    }
  ],
  "serviceDisruptions":{
    "infoMessages":[],
    "importantMessages":[],
    "criticalMessages":[]
  }
}

And then save this to another string which would be displayed on the lxterminal of the raspberry pi 2. I would like only the 'routeName' of B16 to be saved to the string. How do I do that?

umläute
  • 28,885
  • 9
  • 68
  • 122
Dike
  • 33
  • 4
  • 1
    Are you sure this is the whole string? Because there are brackets missing at the end. If this is valid JSON you can deserialize this into a python object: `obj = json.loads(yourstring)` – cansik Aug 31 '15 at 07:05
  • There is more. This is the while string. – Dike Aug 31 '15 at 07:08
  • {"lastUpdated":"08:09","filterOut":[],"arrivals":[{"routeId":"B13","routeName":"B13","destination":"New Eltham","estimatedWait":"1 min","scheduledTime":"07:10","isRealTime":true,"isCancelled":false},{"routeId":"B13","routeName":"B13","destination":"New Eltham","estimatedWait":"29 min","scheduledTime":"07:38","isRealTime":true,"isCancelled":false}],"serviceDisruptions":{"infoMessages":[],"importantMessages":[],"criticalMessages":[]}} – Dike Aug 31 '15 at 07:10
  • And what would I do with this python object if I want the routeName? – Dike Aug 31 '15 at 07:11
  • Please just edit your question to update informations like this. Which `estimatedWait` and `routeName` do you need? Just the first? – cansik Aug 31 '15 at 07:12
  • If an answer helped you, please mark it as answer :) Or write a comment what you are missing. – cansik Aug 31 '15 at 16:17

2 Answers2

0

You just have to deserialise the object and then use the index to access the data you want.

To find only the B16 entries you can filter the arrivals list.

import json
obj = json.loads(json_string)

# filter only the b16 objects
b16_objs = filter(lambda a: a['routeName'] == 'B16',  obj['arrivals'])

if b16_objs:
    # get the first item
    b16 = b16_objs[0]
    my_estimatedWait = b16['estimatedWait']
    print(my_estimatedWait)
cansik
  • 1,924
  • 4
  • 19
  • 39
0

You can use string.find() to get the indices of those value identifiers and extract them.

Example:

def get_vaules(string):
    waitIndice = string.find('"estimatedWait":"')
    routeIndice = string.find('"routeName":"')
    estimatedWait = string[waitIndice:string.find('"', waitIndice)]
    routeName = string[routeIndice:string.find('"', routeIndice)]
    return estimatedWait, routeName

Or you could just deserialize the json object (highly recommended)

import json

def get_values(string):
    jsonData = json.loads(string)
    estimatedWait = jsonData['arrivals'][0]['estimatedWait']
    routeName = jsonData['arrivals'][0]['routeName']
    return estimatedWait, routeName

Parsing values from a JSON file using Python?

Community
  • 1
  • 1
Fyrn
  • 116
  • 1
  • 8