AssetQuery Json

Hi,
im making a GroovyRule and i want to look over a change over a specific parameter in a Json attribute, is it possible to make an AssetQuery to a Json attribute with some type of filter? Thanks

Unfortunately there is no ValuePredicate that can be applied via the standard AssetQuery but the way facts.matchAssetState works is that it calls the ValuePredicate asPredicate method passing in the current attribute value, so you can easily create your own ValuePredicate within your rule like this:

facts.matchAssetState(
   new AssetQuery()
      .attributes(
         new AttributePredicate("test", new ValuePredicate() {
            @Override
            Predicate<Object> asPredicate(Supplier<Long> currentMillisSupplier) {
               return null
            }
         })
      )
)

We already have the json-path library as a dependency in our code so you could import packages within your rule and make full use of the libraries JSON parsing capabilities, or you could use jackson objects to navigate through your value.

Hi Rich,
thanks for the reply. I have no idea how to write a predicate. I need to check the parameter “coordinates” in this Json:

{
  "bat": 4.14,
  "pres": 0,
  "coordinates": [
    -71.30850219726562,
    -41.13875961303711
  ]
}

How can i make it with jackson objects? Thanks

There is already two predicates for dealing with coordinates but they work with Coordinate or GEOJSONPoint objects:

These can easily be used like:

facts.matchAssetState(
   new AssetQuery()
      .attributes(
         new AttributePredicate("test", new RadialGeofencePredicate(radius, lat, lng, negated))
      )
)

Groovy rules provide unlimited ways of achieving things but does require some coding knowledge to get things working, anything you can do in java you can access through groovy so it is a matter of looking at the source code and also documentation of any libraries you’d like to use.

For example going the jackson direction then assuming your attribute value type is JSON Object then the data type of that attribute will be ObjectNode, so you can then call the methods available on that object type to navigate through the JSON (e.g. value.get("coordinates").get(0))