One more question about JSONPATH


I am running OH 2.5.0 on a Windows 10 computer.

I am trying to manipulate some JSON data that I am receiving from a device in my home. As a starting point, I am trying to understand how exactly I should use the JSONPATH transformation. For example, I tried the followind code as part of a rule:

    val TestString = (transform("JSONPATH", '$.cc', '{"aa":"bb", "cc":"dd"}'))
    logInfo("Rules", "test string: {}", TestString)

I was expecting the resulting TestString value would be “dd” but it rather turns out to be the whole untransformed json structure ‘{“aa”:“bb”, “cc”:“dd”}’. I tried several variants of the above without any more success.

Can someone please help me understand the problem?

I’m running 2.5.5 on a Raspberry Pi 3B+

Using exactly the same two lines as you, inside a rule which gets triggered when I switch a proxy switch, I get the correct result:

rule "JSONPath test"
	Item sTestSwitch changed
    val TestString = (transform("JSONPATH", '$.cc', '{"aa":"bb", "cc":"dd"}'))
    logInfo("Rules", "test string: {}", TestString)


20:05:52.691 [INFO ] [.eclipse.smarthome.model.script.Rules] - test string: dd

If I were to guess, your issue is probably with the different " and ’ that you’re using. I wonder whether you might be better off jumping straight to using your actual JSON data, perhaps with the documentation as a guide (though you’ve essentially got code which should work already…). Presumably, as in the example, your JSON string will be stored in an Item?

EDIT: Oh, yeah, as @rossko57 mentions: you have installed the JSONPath add-on, right?

Unusual property of transformation service; if there’s a failure, you get the whole string back.
Note that JSONPATH is an installable add-on, but I’d expect you’d notice a complaint about that in your openhab.log

:@hafniumzinc @rosko OK, problem solved: I had not installed the JSONPath addon. After having done it, the transformation is working fine. Perhaps I should have asked for help before: I wasted quite a bit of time on this. The fact is that: 1) I had seen the documentation page you pointed to, but it does not mention that an addon is needed; and 2) the fact that the addon is missing should result in an error message, but there was none: just an incorrect result. Thanks a lot for your assistance!

You’re right, it’s not mentioned explicitly on that page. However, that page is in the Add-ons section of the documentation. Still, easy to miss!

That sounds strange. What log are you looking in? You know there are two logs? events and openhab

Just played in 2.5.0 , sure enough a nonsense non-installed transform service will not be complained about by transform().

That’s a intentional feature I had not appreciated before. “But this is in the docs!”, haha.
There is an alternate call transformRaw() which does give feedback - but although that works, the current rules validator grizzles about it.

1 Like

Thanks for checking.
On my side I had done a search on the string “JSON” in both ‘events.log’ and ‘openhab.log’ and found no match at all.

1 Like