Working with json files

I’m a running a simple web server

Works great but I am having difficulties read in the data into openhab

The web output looks like this

[{“relay_port”: 4, “x_port”: 0}]

I need 2 x items displaying 1 integer each. The help file say that the output is a json file.

I just want the Intergers displayed.

Sounds terrible but I need help if anyone can spare the time.

Kind regards
Jason

You will need to use a transform, and then you can assign them to an item.

create the transform/relay_port.js file with the below content.

JSON.parse(input).relay_port;

Then reference it in your items file.

Number RelayPort "" { http="<[http://xxx.xxx.xxx.xxx:80/url:60000:JS(relay_port.js)]" }

Then repeat for the second value. Each transform is in it’s own file.

Thank you so much for taking the time to respond. I have followed your post, but I am only reading a “-” when displaying the item.

Just to recap

  1. created the json file in /opt/openhab/configurations/transform.
  2. relay_port.js with line 1 line only

JSON.parse(input).relay_port;

  1. created item

Number RelayPort "" { http="<[http://10.0.1.84:8000/:60000:JS(relay_port.js)]" }

  1. sitemap entry

Text item=RelayPort label="Relay Port [%d]"

BTW the web server in am using is

https://github.com/piface/pifacerelayplus/blob/master/docs/simplewebcontrol.rst

It is awesome!

Kind Regards
Jason

Hi Jason,
hope you waited some time for getting the item updated? the 6000 is the refreshrate in miliseconds.

Start - before getting the item into a Number putting this one into a string item istead to see what the transform gets back at all.

String RelayPort “RelayPort [%s]” { http="<[http://10.0.1.84:8000/:60000:JS(relay_port.js)]" }

In addition it seems that the item type definition is missing in your number item:
Number RelayPort “RelayPort [%d]” { http="<[http://10.0.1.84:8000/:60000:JS(relay_port.js)]" }

cheers
Karsten

Hi Karsten,

Thank you, it did update the type definition as suggested. I also updated the time to 2000 ms. Also change the item names.

The int is displaying “-”
and the String is displaying a “null”

Screen shot of log.

I did manage to get it working with a REGEX, but it seems that it is not the best practice.

Once again, thanks for all the help.

Kind Regards
Jason

I’m no json expert, but I am wondering if it is the square brackets causing the parser to not find the path.

Can you have the webserver return {“relay_port”: 4, “x_port”: 0} instead?

Hi, sorry for the late response. It does seem to be an issue with the [ ]'s

I will have a look and see if I can modify the code to exclude the [ ]'s.

Once again, thank you so much for the assistance and patients.

Kind Regards
Jason

Have you tried jsonpath instead?