txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Parsing Json

Mon Jul 17, 2017 2:22 pm

Any one able to help im trying update some of my scripts that used to grab train times and i am going to use the trainline API but parsing is a bit of a nightmare...

This is my request the times need updating i know which i will do later.

Code: Select all

import requests

session = requests.Session()

rawBody = "{\"adults\":1,\"children\":0,\"destination\":\"SHF\",\"journeyType\":\"Single\",\"origin\":\"LIV\",\"outboundJourney\":{\"time\":\"2017-07-17T15:09:00\",\"type\":\"LeaveAfter\"},\"railcards\":[],\"showCancelledTrains\":true}"
headers = "Accept":"application/json","X-Platform-Type":"Android","X-Consumer-Version":"930","X-Feature":"Plan and buy","Connection":"close","User-Agent":"okhttp/3.4.2","X-Api-Version":"2.0","Accept-Language":"en","X-App-Version":"19.0.0.8013","Content-Type":"application/json; charset=UTF-8"}
response = session.post("https://api.thetrainline.com/mobile/journeys", data=rawBody, headers=headers)

print("Status code:   %i" % response.status_code)
print("Response body: %s" % response.content)


results is this massive bit of json..

Code: Select all

{
  "journeyTickets": [
    {
      "seatAvailabilityCode": "NR",
      "journeyId": 1,
      "ticketId": 1,
      "deliveryOptions": []
    },
    {
      "seatAvailabilityCode": "NR",
      "journeyId": 2,
      "ticketId": 1,
      "deliveryOptions": ["Kiosk"]
    },
    {
      "seatAvailabilityCode": "NR",
      "journeyId": 3,
      "ticketId": 1,
      "deliveryOptions": ["Kiosk"]
    },
    {
      "seatAvailabilityCode": "NR",
      "journeyId": 4,
      "ticketId": 1,
      "deliveryOptions": ["Kiosk"]
    },
    {
      "seatAvailabilityCode": "NR",
      "journeyId": 5,
      "ticketId": 1,
      "deliveryOptions": ["Kiosk"]
    },
    {
      "seatAvailabilityCode": "NR",
      "journeyId": 6,
      "ticketId": 1,
      "deliveryOptions": ["Kiosk"]
    },
    {
      "seatAvailabilityCode": "NR",
      "journeyId": 7,
      "ticketId": 1,
      "deliveryOptions": ["Kiosk"]
    },
    {
      "seatAvailabilityCode": "NR",
      "journeyId": 8,
      "ticketId": 1,
      "deliveryOptions": ["Kiosk"]
    },
    {
      "seatAvailabilityCode": "NR",
      "journeyId": 9,
      "ticketId": 1,
      "deliveryOptions": ["Kiosk"]
    },
    {
      "seatAvailabilityCode": "NR",
      "journeyId": 10,
      "ticketId": 1,
      "deliveryOptions": ["Kiosk"]
    }
  ],
  "journeys": [
    {
      "journeyStatus": "Possible",
      "origin": "WKI",
      "legs": [{
        "trainId": "Y31086",
        "isCancelled": false,
        "serviceProviderCode": "ME",
        "retailTrainIdentifier": "ME1789",
        "origin": {
          "stationCode": "WKI",
          "scheduledTime": "2017-07-17T15:06:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "destination": {
          "stationCode": "LIV",
          "scheduledTime": "2017-07-17T15:38:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "serviceProviderName": "MERSEYRAIL",
        "reservationFlag": "N",
        "busyData": {"coaches": []},
        "seatingClass": "S",
        "transportMode": "Train",
        "id": 1,
        "finalDestinations": ["WKI"]
      }],
      "destination": "LIV",
      "departureDateTime": "2017-07-17T15:06:00.0000000+01:00",
      "id": 1,
      "arrivalDateTime": "2017-07-17T15:38:00.0000000+01:00",
      "direction": "Outbound",
      "walkUpFareCategory": "Peak"
    },
    {
      "journeyStatus": "Possible",
      "origin": "WKI",
      "legs": [{
        "trainId": "Y31089",
        "isCancelled": false,
        "serviceProviderCode": "ME",
        "retailTrainIdentifier": "ME1790",
        "origin": {
          "stationCode": "WKI",
          "scheduledTime": "2017-07-17T15:21:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "destination": {
          "stationCode": "LIV",
          "scheduledTime": "2017-07-17T15:53:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "serviceProviderName": "MERSEYRAIL",
        "reservationFlag": "N",
        "busyData": {"coaches": []},
        "seatingClass": "S",
        "transportMode": "Train",
        "id": 1,
        "finalDestinations": ["WKI"]
      }],
      "destination": "LIV",
      "departureDateTime": "2017-07-17T15:21:00.0000000+01:00",
      "id": 2,
      "arrivalDateTime": "2017-07-17T15:53:00.0000000+01:00",
      "direction": "Outbound",
      "walkUpFareCategory": "Peak"
    },
    {
      "journeyStatus": "Possible",
      "origin": "WKI",
      "legs": [{
        "trainId": "Y31091",
        "isCancelled": false,
        "serviceProviderCode": "ME",
        "retailTrainIdentifier": "ME1791",
        "origin": {
          "stationCode": "WKI",
          "scheduledTime": "2017-07-17T15:36:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "destination": {
          "stationCode": "LIV",
          "scheduledTime": "2017-07-17T16:08:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "serviceProviderName": "MERSEYRAIL",
        "reservationFlag": "N",
        "busyData": {"coaches": []},
        "seatingClass": "S",
        "transportMode": "Train",
        "id": 1,
        "finalDestinations": ["WKI"]
      }],
      "destination": "LIV",
      "departureDateTime": "2017-07-17T15:36:00.0000000+01:00",
      "id": 3,
      "arrivalDateTime": "2017-07-17T16:08:00.0000000+01:00",
      "direction": "Outbound",
      "walkUpFareCategory": "Peak"
    },
    {
      "journeyStatus": "Possible",
      "origin": "WKI",
      "legs": [{
        "trainId": "Y31094",
        "isCancelled": false,
        "serviceProviderCode": "ME",
        "retailTrainIdentifier": "ME1792",
        "origin": {
          "stationCode": "WKI",
          "scheduledTime": "2017-07-17T15:51:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "destination": {
          "stationCode": "LIV",
          "scheduledTime": "2017-07-17T16:23:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "serviceProviderName": "MERSEYRAIL",
        "reservationFlag": "N",
        "busyData": {"coaches": []},
        "seatingClass": "S",
        "transportMode": "Train",
        "id": 1,
        "finalDestinations": ["WKI"]
      }],
      "destination": "LIV",
      "departureDateTime": "2017-07-17T15:51:00.0000000+01:00",
      "id": 4,
      "arrivalDateTime": "2017-07-17T16:23:00.0000000+01:00",
      "direction": "Outbound",
      "walkUpFareCategory": "Peak"
    },
    {
      "journeyStatus": "Possible",
      "origin": "WKI",
      "legs": [{
        "trainId": "Y31096",
        "isCancelled": false,
        "serviceProviderCode": "ME",
        "retailTrainIdentifier": "ME1793",
        "origin": {
          "stationCode": "WKI",
          "scheduledTime": "2017-07-17T16:06:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "destination": {
          "stationCode": "LIV",
          "scheduledTime": "2017-07-17T16:38:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "serviceProviderName": "MERSEYRAIL",
        "reservationFlag": "N",
        "busyData": {"coaches": []},
        "seatingClass": "S",
        "transportMode": "Train",
        "id": 1,
        "finalDestinations": ["WKI"]
      }],
      "destination": "LIV",
      "departureDateTime": "2017-07-17T16:06:00.0000000+01:00",
      "id": 5,
      "arrivalDateTime": "2017-07-17T16:38:00.0000000+01:00",
      "direction": "Outbound",
      "walkUpFareCategory": "Peak"
    },
    {
      "journeyStatus": "Possible",
      "origin": "WKI",
      "legs": [{
        "trainId": "Y31099",
        "isCancelled": false,
        "serviceProviderCode": "ME",
        "retailTrainIdentifier": "ME1794",
        "origin": {
          "stationCode": "WKI",
          "scheduledTime": "2017-07-17T16:21:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "destination": {
          "stationCode": "LIV",
          "scheduledTime": "2017-07-17T16:53:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "serviceProviderName": "MERSEYRAIL",
        "reservationFlag": "N",
        "busyData": {"coaches": []},
        "seatingClass": "S",
        "transportMode": "Train",
        "id": 1,
        "finalDestinations": ["WKI"]
      }],
      "destination": "LIV",
      "departureDateTime": "2017-07-17T16:21:00.0000000+01:00",
      "id": 6,
      "arrivalDateTime": "2017-07-17T16:53:00.0000000+01:00",
      "direction": "Outbound",
      "walkUpFareCategory": "Peak"
    },
    {
      "journeyStatus": "Possible",
      "origin": "WKI",
      "legs": [{
        "trainId": "Y31101",
        "isCancelled": false,
        "serviceProviderCode": "ME",
        "retailTrainIdentifier": "ME1795",
        "origin": {
          "stationCode": "WKI",
          "scheduledTime": "2017-07-17T16:36:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "destination": {
          "stationCode": "LIV",
          "scheduledTime": "2017-07-17T17:08:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "serviceProviderName": "MERSEYRAIL",
        "reservationFlag": "N",
        "busyData": {"coaches": []},
        "seatingClass": "S",
        "transportMode": "Train",
        "id": 1,
        "finalDestinations": ["WKI"]
      }],
      "destination": "LIV",
      "departureDateTime": "2017-07-17T16:36:00.0000000+01:00",
      "id": 7,
      "arrivalDateTime": "2017-07-17T17:08:00.0000000+01:00",
      "direction": "Outbound",
      "walkUpFareCategory": "Peak"
    },
    {
      "journeyStatus": "Possible",
      "origin": "WKI",
      "legs": [{
        "trainId": "Y31104",
        "isCancelled": false,
        "serviceProviderCode": "ME",
        "retailTrainIdentifier": "ME1796",
        "origin": {
          "stationCode": "WKI",
          "scheduledTime": "2017-07-17T16:51:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "destination": {
          "stationCode": "LIV",
          "scheduledTime": "2017-07-17T17:23:00.0000000+01:00",
          "platformStatus": "Realtime",
          "realTimeStatus": "OnTime",
          "platform": "1"
        },
        "serviceProviderName": "MERSEYRAIL",
        "reservationFlag": "N",
        "busyData": {"coaches": []},
        "seatingClass": "S",
        "transportMode": "Train",
        "id": 1,
        "finalDestinations": ["WKI"]
      }],
      "destination": "LIV",
      "departureDateTime": "2017-07-17T16:51:00.0000000+01:00",
      "id": 8,
      "arrivalDateTime": "2017-07-17T17:23:00.0000000+01:00",
      "direction": "Outbound",
      "walkUpFareCategory": "Peak"
    },
    {
      "journeyStatus": "Possible",
      "origin": "WKI",
      "legs": [{
        "trainId": "Y31106",
        "isCancelled": false,
        "serviceProviderCode": "ME",
        "retailTrainIdentifier": "ME1797",
        "origin": {
          "stationCode": "WKI",
          "scheduledTime": "2017-07-17T17:06:00.0000000+01:00",
          "platformStatus": "Scheduled",
          "realTimeStatus": "OnTime",
          "platform": "1 - Scheduled"
        },
        "destination": {
          "stationCode": "LIV",
          "scheduledTime": "2017-07-17T17:38:00.0000000+01:00",
          "platformStatus": "Scheduled",
          "realTimeStatus": "OnTime",
          "platform": "1 - Scheduled"
        },
        "serviceProviderName": "MERSEYRAIL",
        "reservationFlag": "N",
        "busyData": {"coaches": []},
        "seatingClass": "S",
        "transportMode": "Train",
        "id": 1,
        "finalDestinations": ["WKI"]
      }],
      "destination": "LIV",
      "departureDateTime": "2017-07-17T17:06:00.0000000+01:00",
      "id": 9,
      "arrivalDateTime": "2017-07-17T17:38:00.0000000+01:00",
      "direction": "Outbound",
      "walkUpFareCategory": "Peak"
    },
    {
      "journeyStatus": "Possible",
      "origin": "WKI",
      "legs": [{
        "trainId": "Y31109",
        "isCancelled": false,
        "serviceProviderCode": "ME",
        "retailTrainIdentifier": "ME1798",
        "origin": {
          "stationCode": "WKI",
          "scheduledTime": "2017-07-17T17:21:00.0000000+01:00",
          "platformStatus": "Scheduled",
          "realTimeStatus": "OnTime",
          "platform": "1 - Scheduled"
        },
        "destination": {
          "stationCode": "LIV",
          "scheduledTime": "2017-07-17T17:53:00.0000000+01:00",
          "platformStatus": "Scheduled",
          "realTimeStatus": "OnTime",
          "platform": "1 - Scheduled"
        },
        "serviceProviderName": "MERSEYRAIL",
        "reservationFlag": "N",
        "busyData": {"coaches": []},
        "seatingClass": "S",
        "transportMode": "Train",
        "id": 1,
        "finalDestinations": ["WKI"]
      }],
      "destination": "LIV",
      "departureDateTime": "2017-07-17T17:21:00.0000000+01:00",
      "id": 10,
      "arrivalDateTime": "2017-07-17T17:53:00.0000000+01:00",
      "direction": "Outbound",
      "walkUpFareCategory": "Peak"
    }
  ],
  "tickets": [{
    "isPromotional": false,
    "totalFare": 475,
    "routeRestriction": "00000",
    "fareOriginStation": "WEST KIRBY",
    "inboundValidity": {
      "months": 0,
      "days": 0,
      "years": 0
    },
    "fareSource": "O",
    "description": "Travel any time of day.",
    "ticketType": "SDS",
    "outboundValidity": {
      "months": 0,
      "days": 1,
      "years": 0
    },
    "fareCategory": "Peak",
    "fares": [
      {
        "code": "000",
        "passengerType": "Adult",
        "price": 475,
        "numberOfPassengers": 1,
        "fareType": "AdultOrChild"
      },
      {
        "code": "001",
        "passengerType": "Child",
        "price": 0,
        "numberOfPassengers": 0,
        "fareType": "AdultOrChild"
      }
    ],
    "fareRestrictionCode": "",
    "fareDestinationNlc": "0435",
    "fareDestinationStation": "LIVERPOOL STNS",
    "ticketClass": "Standard",
    "fareOriginNlc": "2201",
    "name": "Anytime Day Single",
    "id": 1,
    "throughLondon": false,
    "reservationRequired": false
  }],
  "bookingHorizon": {"Kiosk": 0},
  "routeRestrictions": [{
    "code": "00000",
    "name": "ANY PERMITTED",
    "description": "Travel is allowed via any permitted route."
  }],
  "journeySearchId": 7623819764
}
the information i need is in the journey array.

I am trying to get the time of departure / platform / platformstatus for each ID.

I can easily do this in PHP but python is a bit of a weird one for me any one able to help me parse this?
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

User avatar
elParaguayo
Posts: 1834
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Parsing Json

Mon Jul 17, 2017 2:43 pm

"Journeys" is a list of individual journeys, so you need to loop through that.

Each journey has a list of "legs" so you loop through that too.

Each leg has an "origin" and "destination" each with a dict containing the details you're after.

Assuming you've loaded the json object into a variable (e.g. using "response.json()"), let's call it "rawdata", you could do this:

Code: Select all

for i, journey in enumerate(rawdata["journeys"]):
    print "Journey {}".format(i)
    for j, leg in enumerate(journey["legs"]):
        print "  Leg {}".format(j)
        print "Platform {}\n".format(leg["origin"]["platform"])
This outputs:

Code: Select all

Journey 0
  Leg 0
Platform 1

Journey 1
  Leg 0
Platform 1

Journey 2
  Leg 0
Platform 1

Journey 3
  Leg 0
Platform 1

Journey 4
  Leg 0
Platform 1

Journey 5
  Leg 0
Platform 1

Journey 6
  Leg 0
Platform 1

Journey 7
  Leg 0
Platform 1

Journey 8
  Leg 0
Platform 1 - Scheduled

Journey 9
  Leg 0
Platform 1 - Scheduled
Hopefully you can figure out the rest from there
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

B.Goode
Posts: 3960
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Parsing Json

Mon Jul 17, 2017 2:59 pm

If you haven't already considered it, maybe the python json decoder module would be a cleaner solution than trying to pick apart an apparently random string of characters?

https://docs.python.org/3/library/json.html#module-json

User avatar
elParaguayo
Posts: 1834
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Parsing Json

Mon Jul 17, 2017 3:15 pm

Assuming it's proper json response, the requests module handles this for you. You just use the .json() method on the response object.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

B.Goode
Posts: 3960
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Parsing Json

Mon Jul 17, 2017 3:19 pm

elParaguayo wrote:Assuming it's proper json response, the requests module handles this for you. You just use the .json() method on the response object.
I hadn't understood that - thanks for clarifying.

User avatar
elParaguayo
Posts: 1834
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Parsing Json

Mon Jul 17, 2017 3:20 pm

Also, I never realised the Trainline had an API. Is it open to everybody?
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: Parsing Json

Thu Aug 10, 2017 9:06 am

thank you very much for this!

I will try it now!

Im not sure if its supposed to be open or not but the app uses this so i am using it for now :)
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

Return to “Python”

Who is online

Users browsing this forum: elParaguayo and 17 guests