Http-binding: json parse on empty result

Hello, I’m using 4.0.1 openhab docker image.
I’ve added new thing:

Thing http:url:oref "Red Alert" [ 
	baseURL ="http://mySite.com:1234",
	headers="Content-Type=application/json", "Referer=https://www.mySite.com/", "X-Requested-With=XMLHttpRequest",
	refresh=5] {
		Channels:
			Type switch : aaaSwitch "Alarm" [ stateTransformation="JSONPATH:$.data[?(@ == 'aaa')]" , onValue="aaa", offValue="NULL" ]
}

The result

When the result of the request contains the aaa json it’s okay and turning on the switch, but when the result of the request is a new empty line I’m getting this error in the log:

2023-10-30 18:27:14.366 [WARN ] [.transform.SingleValueTransformation] - Executing transformation ChannelStateTransformation{pattern='$.data[?(@ == 'aaa')]', serviceName='JSONPATH'} failed: Invalid path '$.data[?(@ == 'aaa')]' in '
'

Any idea why?
how can I fix it?

Thanks!

If the response isn’t JSON (e.g. empty) or doesn’t include a JSON that matches the JSONPATH you will get an error.

The MQTT and HTTP binding support transformation chaining so you can do a REGEX transform first to see if the data includes the field you are looking for and then do the JSONPATH. Details are described in the UI or the add-on docs HTTP - Bindings | openHAB

Thank you!
So can I use regex and then JSON parser in the channels of the binding? or should I put it in an item and then use a rule check if it is not null and then return the JSON parse?

Yes, you use it in the Channel, like the docs say.

Thank you, and last how can I add double quotes to the regex?
My regex is: \W("aaa")\W**
but I can’t set it in the channel:
Type text : aaaText “Alarm” [ stateTransformation=“REGEX:\W*("aaa")\W*” ]

Thanks

Remember that all the usual rules in using an OH REGEX transform apply. The expression must match the entire String and the first matching group (i.e. the first set of parens) is what gets returned. So a regex filter being used as a filter like this the pattern usually looks like (.*something unique.*) where “something unique” is a string that only appears in the messages you want to be processed by the next transform in the chain.

At best your current expression will only return “aaa” which isn’t JSON and cannot be processed by the JSONPATH transform.

I want to check if I have the string “aaa” in the response…
so the regex is .“aaa”. but I can’t add the " in the regex :frowning:

Originally you said you either got a message that was empty or a message that could be processed. Therefore only messages that can be processed will have aaa in them. Who cares about the double quotes. You don’t need them.

I could tell you how to do it through the UI (just include the quotes, you need do nothing else). I don’t support file based configs any more. I’d rather spend my time addressing home automation problems, not syntax problems.

But in this case, you don’t need the quotes to be a part of the expression. In fact, if the only unparsable message is empty, (.+.*) will work. Empty messages will be dropped. Any message with one or more character will be passed to the JSONPATH. Since all non-empty messages are parsable by the JSONPATH that’s all you need.