Help with Jsonpath transformation

Hi all!

I am trying to implement a generic MQTT thing, and I need to part a Json string. Unfortunately Jsonpath transformation are not my strength :worried:

The item is defined as follow

String mqttEspNowTestVal2 “Val Test ESPNow [JSONPATH($.device.status.temperature):%s °C]” {channel=“mqtt:topic:54e1b26f:XX-XowTestESPnow”}

To make it the simplest possible, I programmed the remote sender to send the string as provided here

2020-10-30 00:17:59.640 [vent.ItemStateChangedEvent] - mqttEspNowTestVal2 changed from {“device_id”:50:02:91:DC:DC:8F, “value”:81} to [{ “device”: { “location”: “Outside”, “status”: { “temperature”: 23.2 }}}]

Still, the log contains the error

2020-10-30 00:28:27.060 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '[{ "device": { "location": "Outside", "status": { "temperature": 23.2 }}}]' on item 'mqttEspNowTestVal2' with pattern 'JSONPATH($.device.status.temperature):%s °C': Invalid path '$.device.status.temperature' in '[{ "device": { "location": "Outside", "status": { "temperature": 23.2 }}}] °C'

I am sure I am missing something trivial, but I am out of ideas…


It’s the square brackets that you have added, turning your JSON into a one-element array

val rawjson = '[{ "device": { "location": "Outside", "status": { "temperature": 23.2 }}}]'
var results = transform("JSONPATH", "$.device.status.temperature", rawjson)
logInfo("test", " simple path " + results)
results = transform("JSONPATH", "$[0].device.status.temperature", rawjson)
logInfo("test", " array path " + results)
2020-10-30 01:33:25.154 [INFO ] [.eclipse.smarthome.model.script.test] -  simple path [{ "device": { "location": "Outside", "status": { "temperature": 23.2 }}}]
2020-10-30 01:33:25.156 [INFO ] [.eclipse.smarthome.model.script.test] -  array path 23.2
1 Like

Thanks @rossko57!

I have now implemented the complete item with the JSONPATH transformation

 String mqttEspNowTestVal "Val Test ESPNow"    {channel="mqtt:topic:54e1b26f:XX-XowTestESPnow"[profile="transform:JSONPATH", function="$.value"]}

This works as long as the item is defined as String - am I right to say that w/o a rule (like this) I cannot have the item value stored as Number?

If yes, anything going to be different when looking forwards to OH3?

Thanks again!

P.S.: another good JSONPATH validator here

Yes, at OH2 the transform profile is limited to use with String type Items.
More usefully versatile in OH3, I am told.

But …

Why aren’t you using the transform feature built into the MQTT channel, allowing you to output directly to any Item type? That’s what its for.

Treat all JSONPATH online validators with a little caution, the openHAB JSONPATH Transformation Service is not JSONPATH, there are differences in places.

Hi @rossko57

I guess you mean the incoming transformation in the thing definition. I completely missed that, I will give a try and likely post another question :grin: