Experiments in JSON , JSONPATH

Tags: #<Tag:0x00007f6173a540e8> #<Tag:0x00007f6173a54020> #<Tag:0x00007f616c23feb8>

The usual joy of JSON,

I am experimenting with a craftbeerpi installation https://github.com/Manuel83/craftbeerpi3

and queering its “current brew”

String beertest { http="<[$.id)]" }


[{“id”: 1, “name”: “Century Russian Imperial Stout (new)”}]

I would of thought $.id would give : 1 ?

where $.name gives an error of null return

I have tried a selection of approaches to get the $name to a string

latest attempt

var String returnbeertest = transform ("JS", "beername.js", beertest)

(function(i) {
var brewdata = JSON.parse(i);
var brew = brewdata.name[0]
return brew;


2020-04-23 09:52:01.668 [WARN ] [ab.binding.http.internal.HttpBinding] - Transformation ‘JSONPATH($.id)’ threw an exception. [response=[{“id”: 1, “name”: “Century Russian Imperial Stout (new)”}]]
org.openhab.core.transform.TransformationException: Invalid path ‘$.id’ in ‘[{“id”: 1, “name”: “Century Russian Imperial Stout (new)”}]’

some guidance would be greatly appreciated

For valid JSON, it should. Did you add the square brackets to the logging or were they included? Try getting rid of them and then parse as JSON.



String beertest http="<[[$[0].name)]]

works with a JSON feed from a http server

[{“id”: 1, “name”: “Century Russian Imperial Stout (new)”}]

That’s right. It’s because the json that’s returned from the service is an array of length 1. Therefore, $[0].name is the correct way to reference the name element of index 0 of the array.

The reason you got the original string is because $.id doesn’t properly reference index 0 of the array. And, the way the OH JSONPATH implementation works, when there’s no match, the original json string returned.

BTW, the link you posted above doesn’t work for me. Can you double check that that’s the right link?

Speaking of beer… I’m pretty sure Untappd has an API. It would be pretty cool to have openHAB announce what my friends are drinking by polling the friends activity feed. :wink:

1 Like