Parsing Variable Entry JSON Response From HTTP Binding

Hi

I"m trying to use the HTTP Binding to retrieve a JSON feed from a Web API (TFL Bus Arrivals)… However, the JSON response contains a varying number of responses with multiple fields as below…

  {
    "id": "string",
    "operationType": 0,
    "vehicleId": "string",
    "naptanId": "string",
    "stationName": "string",
    "lineId": "string",
    "lineName": "string",
    "platformName": "string",
    "direction": "string",
    "bearing": "string",
    "destinationNaptanId": "string",
    "destinationName": "string",
    "timestamp": "2019-01-08T22:57:30.530Z",
    "timeToStation": 0,
    "currentLocation": "string",
    "towards": "string",
    "expectedArrival": "2019-01-08T22:57:30.530Z",
    "timeToLive": "2019-01-08T22:57:30.530Z",
    "modeName": "string",
    "timing": {
      "countdownServerAdjustment": "string",
      "source": "2019-01-08T22:57:30.530Z",
      "insert": "2019-01-08T22:57:30.530Z",
      "read": "2019-01-08T22:57:30.530Z",
      "sent": "2019-01-08T22:57:30.530Z",
      "received": "2019-01-08T22:57:30.530Z"
    }
  }

There can be as many as 20 “records” returned from the call to the API.

Is there anyway with the HTTP binding to workout how many records have been returned and populate items with the response? I’ve managed to work out how to pick the fields out of each record with a JSONPath transformation using

JSONPATH($[0].lineName

I’ve currently created 4 sets of items for the fields I need using this method, but if there are less than 4 records in the response i see the following errors

org.openhab.core.transform.TransformationException: Invalid path '$[3].lineName

I’m starting to think that this may not be possible using the HTTP binding and may require coding in it’s own binding to manipulate the JSON response and understand the number of records returned.

The API can be accessed at https://api-radon.tfl.gov.uk/StopPoint/490014270P/Arrivals

Thanks

You will probably want to do this in a Rule or outside of OH. JSONPATH is really only going to give you a single element from the JSON and there really isn’t any way to iterate over an indeterminate number of elements from the transform.

I don’t think you need your own binding, but that is certainly a way to go.

I ran into a similar problem with data from NOAA awhile back as I was looking for a replacement for Wundergound. For precipitation it creates N elements of arbitrary length so it takes a bunch of code to aggregate the values into one value for the day.

There is, use a JS transformation