transform("JSONPATH", ) not working for me

  • Platform information:
    • Hardware: RPi4B/8GB/SSD over USB3
    • OS: Raspbian GNU/Linux 10 (buster) with docker. Openhab in container.
    • Java Runtime Environment: openjdk version “1.8.0_265”
    • openHAB version: 2.4.0 (in docker container)

Issue: I try to use the function ‘transform’ in an openhab rule, to get a value out of a json string. I cannot get this function to produce any output other then the json string on input itself.

With the rule below, I run an InfluxDB query for which I didn’t find a pure openhab solution. I had the same challenge and applied the solution as in [closed][unsolved]Influxdb sumSince not working for switches

rule “abc”
when
Time cron “0 * * * * ?”
then
var String answer = sendHttpGetRequest(“http://localhost:8086/query?db=openhab_db&q=select%20sum(value)%20from%20PushButton%20where%20time%20>%20now()%20-5d;”)
val eventCount = transform(“JSONPATH”, “$.results.[0].series.[0].values.[0].[1]”, answer)
logInfo(“rules”, "abc answer: " + answer)
logInfo(“rules”, "abc eventCount: " + eventCount)
end

The http requests replies with a json string and I need to extract one element from this string. In the data below, I need to extract the number 61 out of the json string
These are the generated log data:

2021-01-17 11:59:00.027 [INFO ] [eclipse.smarthome.model.script.rules] - abc answer: {“results”:[{“statement_id”:0,“series”:[{“name”:“PushButton”,“columns”:[“time”,“sum”],“values”:[[“2021-01-12T10:59:00.012434503Z”,61]]}]}]}
2021-01-17 11:59:00.031 [INFO ] [eclipse.smarthome.model.script.rules] - abc eventCount: {“results”:[{“statement_id”:0,“series”:[{“name”:“PushButton”,“columns”:[“time”,“sum”],“values”:[[“2021-01-12T10:59:00.012434503Z”,61]]}]}]}

The jsonpath parsing string seems correct to me. I used the online “Jayway jsonpath evaluator” at https://jsonpath.herokuapp.com/ to validate this and there it works correctly. The result is 61.
I also tried many alternative jsonpath strings. But never obtained any result. The output of the transform function (val eventCount) always remains equal to the input (var answer).

Can anyone advise?

Did you install the JSONPath transformation?

1 Like

Thank you very much Jürgen. I hadn’t noticed that. It’s working now.