BookmarkSubscribe
Choose Language Hide Translation Bar

City Bikes Rest API

For JMP 14, I've been exercising the new HTTP Request object found in JSL.  I came across this API, founds here: http://api.citybik.es/v2/.

This web service can be used to locate City Bike data (and availablilty) world wide.  It has only two endpoints http://api.citybik.es/v2/networks and http://api.citybik.es/v2/networks/[id] so it is a pretty simple Rest API to work with.  The key, though, is how to configure the HTPP Request object to send to the website and then how to process that information.  (Processing the information can be as involved as you want to be)

request = New HTTP Request(URL("http://api.citybik.es/v2/networks"), Method("Get"));
json = request << Send();

This is all that is required to get data from the first endpoint. json is the data that is getting returned from the API.  Since I know this web service, I know the data I should be getting back is Character and it is JSON I'll just check:

!IsEmpty(json)

However, you can check for success and mimetype returned like:

request << Is Successful
request << Get Mime Type == "application/json"

The intial JMP Data Table created from the script is the City Bikes Network table.

It's create from the json data that is returned from the first endpoint.  There are [at least] two ways you can process the returned data.  The first is:

JSON to Data Table(json)

This will try to create a JMP Data Table from the data.  For "rectangular" or "tabluar" data this works really well (and fast).  However, not all webservices return tabular data (City Bikes is one that does not). This brings us to the second way of processing returned data:

Parse JSON(json)

This creates a JSL Associative Array.  This means you don't have to parse the JSON yourself in JSL, but you do, however need to know how the data is structured.  That is you'll need to know if it's a key=>value pair or a key=>values pair.  This isn't as hard as it seems since you can capture the raw json output like:

write(json);

in the log and paste it into your favorite json formatter. (I use JSON Editor Online app in Chrome). This means something like this:

{
  "networks": [
    {
      "company": [
        "Bike U Sp. z o.o."
      ], 
      "href": "/v2/networks/bbbike", 
      "id": "bbbike", 
      "location": {
        "city": "Bielsko-Bia\u0142a", 
        "country": "PL", 
        "latitude": 49.8225, 
        "longitude": 19.044444
      }, 
      "name": "BBBike"
    }, 
...
...
}

Can look like this:

object {1}
    networks {560}
        0 {5}
            company [1]
                  0 : Bike U Sp. z o.o
           href: /v2/networks/bbbike
           id: bbbike
           location {4}
               city: Bielsko-Biaia
               country: PL
               latitude: 49.8225
              longitude: 19.044444
            name: BBBike
...
...

In other words, when you see a [...] in JSON Editor Online, you are dealing with a JSL Array.  When you see a {...}, you are dealing with a JSL Associative Array.

One more note I want to make about the script is that I make use of:

Set Property("Event Handler", ...

So I can drill down into the next endpoint.

When I click on the hyper link in the company column, I use the company_id in the same row to build a new HTTP Request with the url:

"http://api.citybik.es/v2/networks/" || thisTable:company_id[iRow]

That allows me to get more detailed information about the City Bike company and I create another JMP Data Table using the same process as before.

I created a couple of sample table scripts by using Graph Builder and then embedding them in my JSL.

There are many, many more Rest APIs out there, so feel free to give them a try.

Enjoy,

Bryan Boone

 

 

Article Labels
Article Tags
Contributors