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:
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?
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
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”?
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]
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: