JSONPATH transformation error

I have the following code in a rule

val String jsonInfo = sendHttpGetRequest("http://reisapi.ruter.no/StopVisit/GetDepartures/2300444")
logInfo("RAW DATA",jsonInfo)
val String rawBusToOslo = transform("JSONPATH","$.*[?(@.LineRef==\"110\" && @.DestinationName==\"Oslo bussterminal\")][MonitoredCall][DestinationDisplay,ExpectedArrivalTime]",jsonInfo)
logInfo("HENTET DATA",rawBusToOslo)

and get the following error msg

[ERROR] [ore.transform.actions.Transformation] - Error executing the transformation 'JSONPATH': An error occurred while transforming JSON expression.

It is obviously something wrong with the jsonpath transformation (yes, I have installed the transformation service). When I test the jsonpath

$.*[?(@.LineRef=="110" && @.DestinationName=="Oslo bussterminal")][MonitoredCall][DestinationDisplay,ExpectedArrivalTime]

against the same json at

https://codebeautify.org/jsonpath-tester#

It returns what I want.
Any suggestions or tips?

It might be that response or expression itself gets wrongly interpreted because of qutoing. Try increasing log verbosibility and see if you will get any pointers why it fails.
You can do that from console:
log: set DEBUG org.openhab.core.transform.internal.service

Since your code is executed inside rule you can also try to capture exception (via try/catch closures):

try {
    transform(...);
} catch (Exception e) {
    logError("Error while processing response", e);
    // eventually you can inspect e.getCause()
}

Thanks. Good suggestion! I will set log level to debug and see if the exception may provide a bit more clarity.

When i navigate to the request url, the response is XML and not JSON.
Are you sure that thats the correct url for the request?

If yes, you would have to use xpath transformation.

https://docs.openhab.org/addons/transformations/xpath/readme.html

Edit:

Seems that the api can handle several return formats, so maybe there is just a header missing in that request.

Thanks for responding.
Yes, Iā€™ve noticed that the url can return both xml and json. Thatā€™s why I print the returned format to see what Iā€™m actually working with. The header info could for sure be controlling the format returned, but I do not think that this is the problem at hand. However, it could be an opportunity to do the transformation with xml/xpath instead.

@Confectrician
I found that I can get json if the request header parameter Accept = application/json and xml if Accept = application/xml. Thanks for the tip. Iā€™ll investigate if itā€™s easier using xpath.

1 Like