Transform JSONPATH returns whole string

Hi!
can anybody say, why the following code returns the whole variable instead of nothing?
Is there a way to fix this?

var String temp = '{"label1":"asdf"}'
logInfo("TEST",transform("JSONPATH", "$.label", temp,"TEST"))

Output: {“label1”:“asdf”}

Thank you!

Related

Remember, the openAB JSONPATH transformation service is not exactly JSONPATH. For example, it will never return an array.

I think the transform for an invalid path will throw a behind the scenes exception, as well as returning the input string. Do you see a WARN in your openhab.log?
I think you can detect that in a rule with try-catch

EDIT - I had a play but cannot get try-catch to give any help here. You’re left with checking results == source to detect the problem.

'{"label1":"asdf"}'
and
"$.label"

See the difference?

2 Likes

I think the OP set up a deliberate “missing field error” to show how the results are not what you might expect.
JSON sources may have fields that are present only sometimes.
How to deal with that in rules is not obvious.

1 Like

Yes, it returns the original string when it doesn’t find a match. I hate that, BTW. Personally, I wish it would return null. Even an empty string would be better than the returning original string.

1 Like

There are github threads debating at some length, but for now we’re stuck with “thems the transformation rules”.

Thinking on it, it’s actually easy to detect the problem with
If ( sourcestring == resultstring)

So long as you’re not expecting say a MAP to deliberately return identical string.

True, it’s very easy. And this is what I do. However, this wastes CPU, especially with very long JSON strings.

I will create a feature request on github. Do you know if the problem is related to transforms or to jsonpath?

There may already be an issue. I’m not sure. The challenge will be that changing what’s returned will break rules for everyone who does the string equals comparison to determine if there was no match.

I do not know.

Good luck with that. It’s about the “rules of OH transformation”, not JSONPATH in particular.
It may be something that could reconsidered for OH3, but you will not get it for OH2 because it would be a needless breaking change.

Start here

1 Like

More than that, development of openHAB core has moved on to OH 3.0, so there will not be any more updates to OHC in 2.5.x releases. You’ll need to be at least a Contributor of the openHAB Github organization to see this, but there are more details here…

Thank you for the information!