Extract value from json & assign to item

I’m struggling trying to extract a field from what looks like json data but has a slightly different format. This is what I have as a result from a http request:

// [ { “id”: “14199910” ,“t” : “DAX” ,“e” : “INDEXDB” ,“l” : “10,523.07” ,“l_fix” : “10523.07” ,“l_cur” : “€10,523.07” ,“s”: “0” ,“ltt”:“5:45PM GMT+2” ,“lt” : “Oct 12, 5:45PM GMT+2” ,“lt_dts” : “2016-10-12T17:45:00Z” ,“c” : “-54.09” ,“c_fix” : “-54.09” ,“cp” : “-0.51” ,“cp_fix” : “-0.51” ,“ccol” : “chr” ,“pcls_fix” : “10577.16” } ]

I only want to extract the value that refers to field “l” - in this case “10,523.07”. The value should be a assigned to an OpenHAB item that I have defined in my item file.

What is the best way to do this in OpenHAB? I tried http binding but had to give up with the transformation. Neither JSON nor REGEX did deliver what I wanted.

Can anyone please give me some hint how to succeed?

Did you try

JSONPATH($[0].l_fix)

The following REGIX should work:

REGIX(.*l_fix\".*\"([\d\.]*)\".*)

Note, I didn’t test the above so there is likely a typo.

See how to do it in Rules and JavaScript here.

I tried

but no luck. Got lots of error messages. Tested it in a JSONPath Expression tester. It works fine when I manually remove the leading "// " from the beginning of my JSON format. So, I need to find a way how to do this. Can I change the transformation inside the items definition in a way that it “cuts-off” the // before parsing the json file?

Indeed, you can strip those characters off in a rule. You will need a String Item bound to get the full JSON. A Rule to trigger when that Item gets updated. In the rule there are a number of ways to strip off those characters. Probably the easiest would be:

val jsonStr = MyHttpItem.state.toString.replace("// ", ' ')

Thanks, I guess with your help I’m getting a bit closer. But:
How do I get the “full” JSON bound to a String item in the first place? From your previous post I changed my item definition to

String DAX "DAX" {http="<[http://finance.google.com/finance/info?client=ig&q=XETRA%3ADAX:60000:JSONPATH($[0].l_fix)]"}

I understand that the http-binding requires a transformation statement like JSONPATH even before I can work with the returned value in a rule and strip off the unwanted characters. So how do I get the http request bound to an item “the way it comes”?

REGEX((.*))

That will return the full string unchanged. Of course this makes me realize you can strip those characters off in the REGEX itself:

REGEX(// (.*))

Tried it

String DAX  "DAX" {http="<[http://finance.google.com/finance/info?client=ig&q=XETRA%3ADAX:60000:REGEX((.*))]"}

but got following error message:

23:00:22.282 [ERROR] [b.core.service.AbstractActiveService] - Error while executing background thread HTTP Refresh Service
java.util.IllegalFormatConversionException: a != java.util.Date
	at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)[:1.8.0_65]
	at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806)[:1.8.0_65]
	at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753)[:1.8.0_65]
	at java.util.Formatter.format(Formatter.java:2520)[:1.8.0_65]
	at java.util.Formatter.format(Formatter.java:2455)[:1.8.0_65]
	at java.lang.String.format(String.java:2940)[:1.8.0_65]
	at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:150)[211:org.openhab.binding.http:1.9.0.201610090111]
	at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:157)[199:org.openhab.core.compat1x:2.0.0.201610121647]
	at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:173)[199:org.openhab.core.compat1x:2.0.0.201610121647]

Any idea what I did wrong?

That error has nothing to do with this Item. Somewhere you have a DateTime Item or a non-DateTime Item that is using a Date style formatting String in its label. For example, something in a label that looks something like this:

[%1$tm/%1$td %1$tH:%1$tM]

Found the error! I had to double the “%” sign in the http expression to

String DAX  "DAX" {http="<[http://finance.google.com/finance/info?client=ig&q=XETRA%%3ADAX:60000:REGEX((.*))]"}

Now it works with your REGEX. Thanks for your help.