Get value from specific element with JSONPATH

I want to get a value from a specific elemt in this JSON string.

{
	"app_id": "my-sensors",
	"dev_id": "some-name",
	"hardware_serial": "123456789",
	"port": 1,
	"counter": 12,
	"payload_raw": "BWcBEQRobQ==",
	"payload_fields": {
		"relative_humidity_4": 54.5,
		"temperature_5": 27.3
	},
	"metadata":{
  "time": "2018-08-02T08:53:25.208572718Z",
  "frequency": 868.1,
  "modulation": "LORA",
  "data_rate": "SF7BW125",
  "coding_rate": "4/5",
  "gateways": [
    {
      "gtw_id": "eui-aaaaaaaa",
      "timestamp": 2347632747,
      "time": "",
      "channel": 0,
      "rssi": -118,
      "snr": -7.5,
      "rf_chain": 1,
      "latitude": 50.0,
      "longitude": 6.0,
      "altitude": 40,
      "location_source": "registry"
    },
    {
      "gtw_id": "eui-bbbbbbbb",
      "timestamp": 892320427,
      "time": "",
      "channel": 0,
      "rssi": -115,
      "snr": 1.2,
      "rf_chain": 1,
      "latitude": 50.0,
      "longitude": 6.0,
      "altitude": 30
    }
  ],
  "latitude": 50.0,
  "longitude": 6.0,
  "location_source": "registry"
}
}

My item looks like this:

String  LoRa_abc_gw1_id    "GW1 ID: [%s]"  <text>     {mqtt="<[ttn1:my-sensors/devices/some-name/up:state:JSONPATH($.metadata.gateways[?(@.gtw_id == 'eui-bbbbbb').gtw_id)]"}

It works if I just get the first gateway from the array (gateways[0]).
But I want to differenciate the gateways, so I want the values for a specific gateway.

I testet it here https://jsonpath.herokuapp.com/ successfully.

But the problem seems to be that the JSONPATH returns an array

[
   "eui-bbbbbbbb"
]

rather than the value alone.

I tried simply adding [0] to the JSONPATH, but then I get no value returned.

$.metadata.gateways[?(@.gtw_id == 'eui-bbbbbbbb')].gtw_id[0]

[
   
]

Any idea how I can solve this?

Currently I get this error in openhab.log.

2018-08-02 11:18:55.804 [ERROR] [.mqtt.internal.MqttMessageSubscriber] - Error processing MQTT message.
org.openhab.core.transform.TransformationException: An error occurred while transforming JSON expression.
        at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:62)[176:org.openhab.core.compat1x:2.1.0]
        at org.openhab.binding.mqtt.internal.MqttMessageSubscriber.processMessage(MqttMessageSubscriber.java:138)[203:org.openhab.binding.mqtt:1.10.0]
        at org.openhab.io.transport.mqtt.internal.MqttBrokerConnection.messageArrived(MqttBrokerConnection.java:556)[209:org.openhab.io.transport.mqtt:1.10.0]
        at org.eclipse.paho.client.mqttv3.internal.CommsCallback.deliverMessage(CommsCallback.java:475)[209:org.openhab.io.transport.mqtt:1.10.0]
        at org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:379)[209:org.openhab.io.transport.mqtt:1.10.0]
        at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:183)[209:org.openhab.io.transport.mqtt:1.10.0]
        at java.lang.Thread.run(Thread.java:748)[:1.8.0_152]

$.metadata.gateways[?(@.gtw_id == 'eui-bbbbbbbb')].gtw_id

Returns "eui-bbbbbbbb"

It looks like an arrays in the website and also in jsonpath.com
but the JSONPATH transform will give you the string on its own.

$.metadata.gateways[?(@.gtw_id == 'eui-bbbbbbbb')].latitude
Returns: 50 as expected

Thanks.

But it still does not work.

2018-08-02 17:22:17.727 [ERROR] [.mqtt.internal.MqttMessageSubscriber] - Error processing MQTT message.
org.openhab.core.transform.TransformationException: An error occurred while transforming JSON expression.
        at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:62)[176:org.openhab.core.compat1x:2.1.0]
        at org.openhab.binding.mqtt.internal.MqttMessageSubscriber.processMessage(MqttMessageSubscriber.java:138)[203:org.openhab.binding.mqtt:1.10.0]
        at org.openhab.io.transport.mqtt.internal.MqttBrokerConnection.messageArrived(MqttBrokerConnection.java:556)[209:org.openhab.io.transport.mqtt:1.10.0]
        at org.eclipse.paho.client.mqttv3.internal.CommsCallback.deliverMessage(CommsCallback.java:475)[209:org.openhab.io.transport.mqtt:1.10.0]
        at org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:379)[209:org.openhab.io.transport.mqtt:1.10.0]
        at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:183)[209:org.openhab.io.transport.mqtt:1.10.0]
        at java.lang.Thread.run(Thread.java:748)[:1.8.0_152]
String  LoRa_ZO0315xxxxx_gw1_id              "GW1 ID: [%s]"                  <text>                  (gLoRa)         {mqtt="<[ttn1:my-sensors/devices/xxxxxxxxx/up:state:JSONPATH($.metadata.gateways[?(@.gtw_id == 'eui-bbbbbbbbb').g
tw_id)]"}

Try:

$.metadata.gateways[?(@.gtw_id==eui-bbbbbbbb)].gtw_id

without the spaces and the single quotes

Doesn’t work either. :frowning:

Are you running an OH snapshot build? If so, indefinite paths are no longer able to be used with the JSONPATH transformation service for item channel configs, if they return a list with more than one element…

EDIT: I forgot how old this was… this change was included in OH 2.3 release! I’m surprised that you are getting an error though.

I am still using openHAB 2.1. :see_no_evil:

Could that be the problem?

You’re definitely not affected by this! Actually, I think if you upgraded to 2.3, what you had will work. That was what the ESH change was for… when single element lists are returned from the transform. You can find a link to the PR in the post I shared. Since you’re only getting one element in the list, this should work for you. The change broke every other use case for indefinite paths.