Jsonpath transform in OH2 http binding

So much simpler than I expected, for anyone else who is interested!

rule "Get Power Usage"
    when 
        //Item TestSwitch changed to ON 
        Time cron "0 * * * * ?"
    then       
        var String json = sendHttpGetRequest("http://NeurioIP/current-sample")
        var String value = transform("JSONPATH", "$.channels[2].p_W", json)
        logInfo("NEURIO","************* Current Usage: " + value )
        postUpdate(power_Neurio, value)      
end
Number	power_Neurio	"Current Power Usage [%.1f W]"
1 Like

Hello!

I’m having a problem using JSONPath to retrieve value of a JSON node which contains blanks in the node name. This is the part of the JSON result I’m struggling to parse:

\"Indeks kvaliteta zraka\":\"386\"

As you can see, it contains an empty spaces in the name, so, when I try to parse it using JSONPath, it returns the whole JSON result, not just a result of the node I want. I’ve tried surrounding it with square brackets and single quotation marks, but without success. Is there a way to parse it using JSONPath?

Best regards,
Davor

I’ve managed to find a way to parse it. It should be surrounded by square brackets AND single quotation marks.

$.cities[0].city.[‘Indeks kvaliteta zraka’]

Best regards,
Davor

Thanks! Had similar problem with Heatmiser NeoHub using json data with spaces. :slight_smile:

For the record: https://github.com/json-path/JsonPath#what-is-returned-when
This seems to match what I see in my setup. I get scalars for simple queries but as soon as I include .. or ?(<expr>) an array is returned even if there is one item.

Also some discussion here: https://github.com/openhab/openhab1-addons/issues/4768