these is my rule to extract for example the first value {"Rai 1":{"ora":"21:25", very simple … I tought, but not works
var String json = sendHttpGetRequest("http://api.silveros.it/tv.php")
var String value = transform("JSONPATH", "$.Rai 1.ora", json)
logInfo("Json",value)
Instead of 21:25. Log monitor give me back the whole string no pharsed at all.
If you have control over what ever is generating this JSON, correct that to not use a space in the key. Use “Rai1” or “Rai_1” instead. In all likelihood you do not have this control but I mention this for future readers. Do not put spaces in JSON keys.
Because there is a space there you can’t use a straight forward JSONPATH. Try
The Json is not mine, is on a public page and I will try to search something better knowing that is a rule and the programmer did not respect it, but could be not simple try something suitable for me.
anyway I try also "$[Rai 1].ora" or "$.[Rai 1].ora"
and did not work, same behaviour
but var String value = transform("JSONPATH", "$.Rete4.ora", json)
which Rete4 is withous spacers, it works and give me back only the time.
I’m going to search an alternative sorce of Json,
Thanks rlkoshak
I’m actually not really able to verify my idea, but you might give it a try - is the third parameter of the transform(..) function expected to be a string, or a json?
By trying your webservice json here https://jsonpath.com/ it worked in several variations like $[Rai 1].ora or $['Rai 1']['ora'].
As I mentioned, I didn’t try scripting in OH yet, but in JavaScript, I’d try it with transform("JSONPATH", "$.Rete4.ora", JSON.parse(json))
The third argument to transform is always a String. sendHttpGetRequest also always returns a String (or null if there was a problem).
Note, transform is an openHAB Action. All interactions with openHAB, be it sending commands, creating timers, calls to actions, are done through interactions with Java Objects no matter what rules scripting language is being used. This line will return an error because the Java Object that implements the transform Action doesn’t know a thing about a JavaScript JSON Object and wouldn’t know how to handle it.
Sure, you can do anything you want since you are working in a rule. But it gets complicated if that one space isn’t always in the same place, isn’t always there, etc. Probably the easiest/safest would be to replace that part of the String.
json.replace("Rai 1", "Rai1")
It doesn’t have to be JSON. OH also has XML transforms and XPath works a whole lot like JSONPATH (in fact XPath came first).
One last question to @rossko57 that seems to me very expert in that field:
If I want to transform the Json, based on an array of strings, what is the correct way?
i’m trying something like (but could be not the right and easiest way)
var List<String> canali = newArrayList("Rai 1", "Rete4", so on...)
var i = 0
while ((i=i+1) < 1) {
var String value = transform("JSONPATH", "$.['" + canali[i] + "'].programma", json)
logInfo("Json",value)
}
It is a very hard problem at my current state of knowledge.
If you could help me I will be glad to you =)
The JSONPATH Transformation Service is limited in some respects, an important limitation is that you can only return one string. There is no way to return an array.
There are some “cheats” that can be used in some circumstances to get a single string with multiple content.
That doesn’t seem to have anything to do with your while-loop.
Show us your example JSON, show us what you want to extract, I’m not guessing.