Transformation JSONPATH doesn't work

(Jacob Naberezhnev) #1

Hi everybody. I started to study OpenHAB2, but don’t understand how to work with JSON transformation.
In Paper UI, “JSONPath Transformation” is installed.
I wrote python script, that return string:

{“fw”: “3.1.8”,“name”: “Flower care”,“temperature”: 24.8,“moisture”: 11.0,“light”: 79.0,“conductivity”: 43.0,“battery”: 99.0}

then I add Item:

String miFlora "Name [JSONPATH($.name)]" (All) {channel="exec:command:miFlora:output"}

When I try to see control, I look in Commad panel this text: JSONPATH($.name).
There is an impression that JSONPATH doesn’t work correctly.
Please tell me how to solve the problem? Or any recommendations on what to read are very welcome.

(Josar) #2

Following the docs you need to place a %s formatter to define that the return value from the jsonpath request is placed as string to the label.
I think your Item should look like this:

String miFlora "Name [JSONPATH($.name):%s]" (All) {channel="exec:command:miFlora:output"}

But i would prefere the moisture and here i also show how to set a formatter for a floating point value.
And that you need to escape the % with an %.

String miFlora "Name [JSONPATH($.moisture):%.1f %%]" (All) {channel="exec:command:miFlora:output"}

(Jacob Naberezhnev) #3

Thanks for your reply. I try to add “:%s”, but result was unexpected. In control panel appear all string such: { “fw”: “3.1.8”, “name”: “Flower care”, “temperature”: 24.0, “moisture”: 10.0, “light”: 61.0, “conductivity”: 37.0, “battery”: 99.0 }.
I check syntax JSON FORMAT, all this right.
Any idea why?

(Josar) #4

Please try to use code fences, this makes it a lot better to read. Just use the buttons above the type area ore type the three back-ticks, thank you.

your code goes here

@MrYahin I think you use the wrong quotes.

    "fw": "3.1.8",
    "name": "Flower care",
    "temperature": 24.8,
    "moisture": 11.0,
    "light": 79.0,
    "conductivity": 43.0,
    "battery": 99.0

Query $.name works then. Try it out

Maybe you could also give the bluetooth binding a try.