Project Help - Parsing MTA Bustime data from a json request

I’ve been using particle photons in a few projects so far and this forum has been great. For my next project, I wanted to build something similar to the following project that would tell me how many stops away my bus is in the morning by using the NYC MTA Bustime API:

I’m not quite sure how I would go about parsing the json data I get from my request in the cloud. Below is a sample of the request and resulting data:

Request:
I can make requests with my API key and get results in either json or xml ( http://bustime.mta.info/wiki/Developers/SIRIStopMonitoring)

For example, the below request gives me the results for two buses that are approaching the designated stop code 402205.

http://bustime.mta.info/api/siri/stop-monitoring.json?key=MY KEY&OperatorRef=MTA&MonitoringRef=402205&LineRef=MTA%20NYCT_M57&MaximumStopVisits=2

Data:
In the below data, I would like to be able to extract “StopsFromCall” for each of the two buses. This would tell me the number of stops away from stop code 402205 that each bus is.

{"Siri":{"ServiceDelivery":{"ResponseTimestamp":"2016-03-26T11:07:43.011-04:00","StopMonitoringDelivery":[{"MonitoredStopVisit":[{"MonitoredVehicleJourney":{"LineRef":"MTA NYCT_M57","DirectionRef":"0","FramedVehicleJourneyRef":{"DataFrameRef":"2016-03-26","DatedVehicleJourneyRef":"MTA NYCT_MQ_A6-Saturday-066300_M57_8"},"JourneyPatternRef":"MTA_M570093","PublishedLineName":"M57","OperatorRef":"MTA NYCT","OriginRef":"MTA_400751","DestinationRef":"MTA_903094","DestinationName":"EAST SIDE YORK - 60 ST XTOWN","SituationRef":[],"Monitored":true,"VehicleLocation":{"Longitude":-73.984209,"Latitude":40.779693},"Bearing":157.21857,"ProgressRate":"normalProgress","BlockRef":"MTA NYCT_MQ_A6-Saturday_A_MQ_21840_M57-2","VehicleRef":"MTA NYCT_3914","MonitoredCall":{"ExpectedArrivalTime":"2016-03-26T11:14:58.735-04:00","ExpectedDepartureTime":"2016-03-26T11:14:58.735-04:00","Extensions":{"Distances":{"PresentableDistance":"0.8 miles away","DistanceFromCall":1281.37,"StopsFromCall":6,"CallDistanceAlongRoute":1449.27}},"StopPointRef":"MTA_402205","VisitNumber":1,"StopPointName":"W 57 ST/11 AV"},"OnwardCalls":{}},"RecordedAtTime":"2016-03-26T11:07:22.000-04:00"},{"MonitoredVehicleJourney":{"LineRef":"MTA NYCT_M57","DirectionRef":"0","FramedVehicleJourneyRef":{"DataFrameRef":"2016-03-26","DatedVehicleJourneyRef":"MTA NYCT_MQ_A6-Saturday-067800_M57_11"},"JourneyPatternRef":"MTA_M570093","PublishedLineName":"M57","OperatorRef":"MTA NYCT","OriginRef":"MTA_400751","DestinationRef":"MTA_903094","DestinationName":"EAST SIDE YORK - 60 ST XTOWN","OriginAimedDepartureTime":"2016-03-26T11:18:00.000-04:00","SituationRef":[],"Monitored":true,"VehicleLocation":{"Longitude":-73.98418,"Latitude":40.779681},"Bearing":157.21857,"ProgressRate":"noProgress","ProgressStatus":"layover","BlockRef":"MTA NYCT_MQ_A6-Saturday_A_MQ_20040_M57-1","VehicleRef":"MTA NYCT_6737","MonitoredCall":{"ExpectedArrivalTime":"2016-03-26T11:14:53.396-04:00","ExpectedDepartureTime":"2016-03-26T11:14:53.396-04:00","Extensions":{"Distances":{"PresentableDistance":"0.8 miles away","DistanceFromCall":1284.12,"StopsFromCall":6,"CallDistanceAlongRoute":1449.27}},"StopPointRef":"MTA_402205","VisitNumber":1,"StopPointName":"W 57 ST/11 AV"},"OnwardCalls":{}},"RecordedAtTime":"2016-03-26T11:07:16.000-04:00"}],"ResponseTimestamp":"2016-03-26T11:07:43.011-04:00","ValidUntil":"2016-03-26T11:08:43.011-04:00"}],"SituationExchangeDelivery":[]}}}

I was hoping someone might be able to point me in the right direction on what my options are for parsing this data. I’m rather new at this sort of stuff and I’m learning as I go. I’d like it to be done in the cloud or on the particle photon if it is possible. I’d want to be able to refresh every 15-30 seconds or so. I was also thinking I could possibly run a python script on a raspberry pi to handle this but I was hoping to create a stand alone project.

So, I am trying to work through this and I found the following post/tutorial that is very helpful:

I was able to create the following webhook template and so far it seems to be working when I publish the web hook through the particle CLI and review the output in the particle dashboard. I’m going to create some test code next to run on the particle photon. I’m crossing my fingers and I hope that my template below is correct.

{
  "event": "bustime_hook",
  "url": "http://bustime.mta.info/api/siri/stop-monitoring.json?key=MY_KEY&OperatorRef=MTA&MonitoringRef=402205&LineRef=MTA%20NYCT_M57&MaximumStopVisits=1",
  "requestType": "GET",
  "headers": null,
  "query": null,
  "responseTemplate": "{{#Siri}}{{#ServiceDelivery}}{{#StopMonitoringDelivery}}{{#MonitoredStopVisit}}{{#MonitoredVehicleJourney}}{{#MonitoredCall}}{{#Extensions}}{{Distances.StopsFromCall}}{{/Extensions}}{{/MonitoredCall}}{{/MonitoredVehicleJourney}}{{/MonitoredStopVisit}}{{/StopMonitoringDelivery}}{{/ServiceDelivery}}{{/Siri}}",
  "json": null,
  "auth": null,
  "mydevices": true
}

Dashboard output:

1 Like

Okay, so far I’m pretty happy with the results I have been getting. I was able to get a proof of concept running with my “bustime clock” project. I ultimately went with the below webhook that returns the number of stops away that the specified bus line is from a given stop. It will return a maximum number of three buses. So for example, if three buses are in the queue, my particle will return a string similar to the following “~2~5~8” which I can easily parse into variables and use them in my program.

This seems to be a good start however I have to use a different webhook for each busline and stop number. At the moment, I created three different webhooks. I’d prefer to replace the three web hooks with one that is more dynamic and would allow me to vary the following by sending data when it is published:

  1. Vary the stop number (MonitoringRef). The stop number in the template below is the “402205”
  2. Vary the line (LineRef). The line in the template below is “NYCT_M12”.

I’d imagine this is possible but I’m not sure what the next steps would be. Any help to point me in the right direction would be appreciated.

Current working webhook example

{
  "event": "bustime12E_hook",
  "url": "http://bustime.mta.info/api/siri/stop-monitoring.json?key=MY_KEY&OperatorRef=MTA&MonitoringRef=402205&LineRef=MTA%20NYCT_M12&MaximumStopVisits=3",
  "requestType": "GET",
  "headers": null,
  "query": null,
  "responseTemplate": "{{#Siri}}{{#ServiceDelivery}}{{#StopMonitoringDelivery}}{{#MonitoredStopVisit}}{{#.}}~{{#MonitoredVehicleJourney}}{{#MonitoredCall}}{{#Extensions}}{{Distances.StopsFromCall}}{{/Extensions}}{{/MonitoredCall}}{{/MonitoredVehicleJourney}}{{/.}}{{/MonitoredStopVisit}}{{/StopMonitoringDelivery}}{{/ServiceDelivery}}{{/Siri}}",
  "json": null,
  "auth": null,
  "mydevices": true
}

I think you’ll need to look at custom template variables
https://docs.particle.io/guide/tools-and-features/webhooks/#custom-template-variables

What @ScruffR said and… Everything after the question mark is a query. You can use the query section along with variables you can pass in your webhook publish.

1 Like

Thanks very much. I have it working now after testing in the CLI.

CLI
particle publish bustime_hook “{ “my-stop”: “400744”, “my-bus”: “M57” }”

Webhook

  {
      "event": "bustime_hook",
      "url": "http://bustime.mta.info/api/siri/stop-monitoring.json?key=MY_KEY&OperatorRef=MTA&MonitoringRef={{my-stop}}&LineRef=MTA%20NYCT_{{my-bus}}&MaximumStopVisits=3",
      "requestType": "GET",
      "headers": null,
      "query": null,
      "responseTemplate": "{{#Siri}}{{#ServiceDelivery}}{{#StopMonitoringDelivery}}{{#MonitoredStopVisit}}{{#.}}~{{#MonitoredVehicleJourney}}{{#MonitoredCall}}{{#Extensions}}{{Distances.StopsFromCall}}{{/Extensions}}{{/MonitoredCall}}{{/MonitoredVehicleJourney}}{{/.}}{{/MonitoredStopVisit}}{{/StopMonitoringDelivery}}{{/ServiceDelivery}}{{/Siri}}",
      "json": null,
      "auth": null,
      "mydevices": true
    }

Dashboard

1 Like