Help with parsing data

For a project that I’m doing at school, I need to take bus information from the website, parse the data, and then display it on a LCD matrix screen. I have the LCD matrix figured out, but I can use some help with getting the data. Using a route 26 bus for example, the web url would be, http://transport.tamu.edu/BusRoutesFeed/api/route/26/buses/mentor. According to the IT professional I talked to, the data can either be in XML data format or JSON format. I need the estimated depart time data from the web page.

That said, would the code be something like this? I used some of the sample code on the website.

local request=http.get(“http://transport.tamu.edu/BusRoutesFeed/api/route/26/buses/mentor","Accept: application/json”)
function handleResponse(responseTable) {
if (responseTable.statuscode == 200) {
local Random_variable=http.jsondecode(request.body)
Some_variable={Random_variable}[0]NextStops[0].EstimatedDepartTime
} else {
server.log("Error response: " + responseTable.statuscode);
}
}
request.sendasync(handleResponse);

I tried the first line in the code, but I get an error with the header. What’s the proper way to do a header?

I’m sorry for the trouble guys. My major is Electrical Engineering so unfortunately I know very little about Web programming. Any help at all is greatly appreciated. Thanks!

It should local request=, not
local
request=
Sorry about the confusion.

The second argument of http.get is a optional table of headers, ie you should have specified:
local request=http.get("http://transport.tamu.edu/BusRoutesFeed/api/route/26/buses/mentor",{ "Accept" : "application/json" } )

I don’t quite understand the code you have underneath that.
If you’ve checked that responseTable.statuscode is ok, then I assume you will JSON decode responseTable.body, not request.body?

The first thing you should look to do with your code is to check what you receive. Try using server.log(“body=”+responseTable.body) in your function.

Simply putting this URL in a browser should yield some sort of information, whether it’s XML or JSON:
http://transport.tamu.edu/BusRoutesFeed/api/route/26/buses/mentor

Is that the complete URL?
or should there be more to it?

That specific web page has data as long as buses are running during the day and night from about 7 am-1 am. If it’s earlier than that, nothing will show. If you’re to check now, data should be there. Just for the information, the official website with all of the routes, times, etc. is http://transport.tamu.edu/busroutes.

Thanks for the advice, I’ll give it a shot and see what happens.

As for the “function handleResponse” bit of code where it checks the status, every example of http code I found seemed to have it, so I included it. Not sure if it’s needed or not.

Yes, it’s very important. It’s also good practice to wrap a http.jsondecode() in a try{}catch{}

`function handleResponse(responseTable) {
  if (responseTable.statuscode == 200){
    try {
      local data=http.jsondecode(responseTable.body);
      // do stuff with data here....
    }
    catch(ex) {
      server.log("Exception decoding JSON: "+ex);
    }
  }
  else
  {
    server.log("Request Failed - statuscode="+responseTable.statuscode);
  }
}`

The URL you provided is XML. You need to find out what the URL would be for JSON.
Also, what particular info are you trying to get out of it.

it may work if the site responds appropriately to the included header: “Accept”:“application/json”

The site with the actual JSON feed is http://transport.tamu.edu/BusRoutesFeed/swagger/ui/index#!/Buses/Buses_GetBusesOnRoute_Mentor. It should already have “application/json” selected so adding 26 to where the route is required should show the data I need in JSON format.

With that said, thanks for the help so far. Here’s the code I have at this point:
`

local request=http.get(“http://transport.tamu.edu/BusRoutesFeed/api/route/26/buses/mentor”,{ “Accept” : “application/json” } )
function handleResponse(responseTable)
{
if (responseTable.statuscode == 200)
{
server.log(“body=”+responseTable.body)
local random_variable=http.jsondecode(responseTable.body)
server.log(random_variable)
} else
{
server.log("Error response: " + responseTable.statuscode);
}
}
request.sendasync(handleResponse);
`

The first server.log shows the JSON data and the second server.log shows “array:0x7f2037b04810” or something similar to it so I’m assuming it’s working correctly.

The bulk of my project requires that I search through the indices of the JSON array to find the bus that has the name “Trigon” within the NextStop array and then grab that bus’s EstimatedDepartTime field. Ignoring that I have to iterate through the array for right now, would the code for pulling the EstimatedDepartTime field be something like this?
local estimated_depart_time={random_variable}[index].NextStops[index].EstimatedDepartTime

Also, thanks for the tip about the try-catch as well. I’ll add all of the fancy stuff at the end when everything works.