Json transformation - help needed

I need to ask you help 'cause my infinite ignorance :slight_smile:

I need to pharse a simple Json string. I try and try, but no luck.

I try also to re-install Jsonpath transormation 2 times…

These is the URL for the Json

these is my rule to extract for example the first value {"Rai 1":{"ora":"21:25", very simple … I tought, but not works :frowning:

var String json = sendHttpGetRequest("http://api.silveros.it/tv.php")
var String value = transform("JSONPATH", "$.Rai 1.ora", json)

Instead of 21:25. Log monitor give me back the whole string no pharsed at all.

Can you do better ?

Many thanks to my saver!

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

"$[Rai 1].ora"

as the path.

1 Like

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

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))

Maybe that helps!

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.

Thank you @rlkoshak for your explanation. Wasn’t aware of this architecture.

Pitty it doesn’t help! :slight_smile:

The problem at the end is the space in the Key.
Is it possible to remove Key spacing after getHTTPreq and before transformation?

I really didn’t find other free tv program as Json service

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).

That is normal, this is what you get when transformation fails.

As said, this JSON is malformed.

But try
“$.[‘Rai 1’].ora”
with the single quotes


1 Like

Yes, that is the syntax that correctly works!

many thanks.

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)

It is a very hard problem at my current state of knowledge.
If you could help me I will be glad to you =)

many thanks in advance

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.

There’s a complicated list example here