JsonPath transformation error handling

I am extracting the value b from the JsonPath expression $.a.b. The value is dispayed on a sitemap.

However, it can happen that value a is not present in the json string to be parsed (and therefore neither b). In that case the whole json string is returned, which is according to the documentation:

If the JsonPath expression provided results in no matches, the transformation will return the entire original JSON string.

But this is obviously not what I want because in this case I want nothing displayed on the sitemap instead of the whole string.

Any idea how I can handle this error properly ? If the value is present in the json it should be displayed, otherwise nothing (or “not present”).

Thanks

If this is the MQTT or the HTTP binding you can chaine transformations. So you can put abregex in from that only matches when b is present and that will cause the messages that don’t match to be ignored.

Otherwise you’ll have to use a rule and proxy item.

Yes, I’m using the HTTP binding for this.
I get your idea but I am not sure how to do this exactly. Could you provide a simple example?
Thanks

Thanks for the reference.

Unfortunately it looks like JsonPath does not like an empty json string. Filtering the json string with regex (returning nothing if the parameter I am looking for is not present) and passing it to JsonPath throws this error:

java.lang.IllegalArgumentException: json string can not be null or empty

Please show us your configuration.

Do you have the REGEX Transformation Service installed?

Yes, REGEX and JSONPATH transformation services are installed.

I did some changes, now I don’t have the error message anymore, but if “b” is not present I still get the whole json string as channel value.

This is the channel configuration:


  - id: http_mode
    channelTypeUID: http:string
    label: Mode
    description: ""
    configuration:
      mode: READONLY
      stateTransformation: REGEX:(.*b.*)∩JSONPATH:$.a.b

I guess the problem is this:

What version of OH are you using. It has long since changed back to return nothing when there is no match.

My OH version is 3.1.

Maybe my regular expression is wrong. The syntax is really mind boggling. I want the whole string returned if the substring matches, otherwise null. But it doesn’t seem to work with the current configuration.

To is little by little. Use a String Channel and just have the REGEX transform and see exactly what it’s doing. Consider that, assuming your JSON literally has a “b” element in it (in which case what ever is producing this JSON is doing it wrong), that REGEX is going to match any message where the letter “b” appears anywhere in the message so it might be matching messages without the “b” element but a word somewhere that includes “b”.

If you are redacting and editing your configs for our benefit, please don’t. We can handle complexity. We are really good at ignoring the parts that are not relevant. And most importantly of all, we know better than you do what is actually relevant. There is a pretty good chance here that you’re editing of your actual config to make it simpler for us is actually hiding the root cause of your problem. So please post an actual JSON message and your actual HTTP Thing configuration.

It’s tricky:

                          _   _     _     ____   _
  ___   ___   ___   ___  | | | |   / \   | __ ) (_)  ____   ___
 / _ \ / _ \ / _ \ / _ \ | |_| |  / _ \  |  _ \ | | / _  \ / _ \
| (_) | (_) |  __/| | | ||  _  | / ___ \ | |_) )| || (_) || | | |
 \___/|  __/ \___/|_| |_||_| |_|/_/   \_\|____/ |_| \__|_||_| | |
      |_|                  openHAB 3.4.0-SNAPSHOT - Build #3224

REGEX:

REGEX:s/.*ol":"(.*)","Bits":(.*),"Data":"(.*)","DataLSB.*}}/$1_$2_$3/g

Tasmota Console:
18:44:42.415 MQT: tele/tasmota_S06/RESULT = {"IrReceived":{"Protocol":"RC5","Bits":12,"Data":"0xC","DataLSB":"0x30","Repeat":0}}

Item:
grafik

Tasmota Console:
18:46:19.583 MQT: tele/tasmota_S06/RESULT = {"IrReceived":{"Protocol":"UNKNOWN","Bits":6,"Hash":"0xE4189B25","Repeat":0}}

Item:
grafik

Working as designed or wrong result? IMHO undecidable on basis of the documentation:
RegEx - Transformation Services | openHAB - but I’d tend to ‘wrong result’, result should be NULL.

Exactly, seems that a “try and error” approach is the only thing to do here. When it could be so easy if only the JsonPath transformation wouldn’t return the whole string if there is no match (which really doesn’t seem to be a reasonable behaviour).