QH2 test JSON part for null?

I am getting an error, which makes sense, because I am looking for a part called ‘vel’ in an json string sent by OwnTracks. OwnTracks does not send an empty ‘vel’ in case the tracker is not moving, but simply omits the item ‘vel’ altogether.

The rule is:

      val String vel = transform("JSONPATH", "$.vel", json)
      GeoLoc_MaxG_Vel.postUpdate(vel)

which works as long as vel is part of the json string; otherwise I get:

2019-08-19 21:30:25.574 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert '{"t":"u","tst":1566213829,"acc":14993,"_type":"location","alt":188,"lon":152.3957915415919,"vac":20,"lat":-27.320936999704188,"batt":100,"conn":"m","tid":"MG"}' to a state type which item 'GeoLoc_MaxG_Vel' accepts: [DecimalType, QuantityType, UnDefType].

How can I test if vel is present?

I tried this, but it does not do what I expect.

      if (transform("JSONPATH", "$.vel", json) === null) {
        logInfo("Geo.0.1", "check vel for null.....: {}", "in if")
        val String vel = "0"
      } else {
        logInfo("Geo.0.1", "check vel for null.....: {}", "in else")
        val String vel = transform("JSONPATH", "$.vel", json)
      }

      logInfo("Geo.0.1", "check vel for null.....: {}", vel)

It actually omits that if statement altogether (as I do not get the log entries).

Any hints appreciated.

There has been more than recent thread about similar JSON transforms with potentially missing fields, but in the context of bindings.
The siggested technique of pre-scanning for the firld using REGEX should work on a rule too.

1 Like