How to use AssetQuery to query assets via HTTP API

Attributes field takes a LogicGroup<AttributePredicate>; LogicGroups allow for AND/OR logic operations with grouping (hence the name) this allows things like:

OR(AND(Attribute1Value1, Attribute2Value1), AND(Attribute1Value2, Attribute2Value2))

Which would look something like this in code:

(Attribute1Value1 && Attribute2Value1) || (Attribute1Value2, Attribute2Value2)

In your case if you’re only interested in one Attribute then you are on the right track, just need to add the value predicate the list of value predicates can be found at the top of ValuePredicate.java; so for example if it’s a boolean attribute and you want to find assets with a true value:

{
  "realm": {
    "name": "master"
  },
  "attributes": {
    "items": [
      {
        "name": {
          "match": "EXACT",
          "value": "meter_number",
          "predicateType": "string"
        },
        "value": {
           "predicateType": "boolean",
           "value": true
        }
      }
    ]
  }
}

Many of the predicates support negation if required (e.g. string attribute is not exactly equal to OK); also if your attribute value type is a JSON array or JSON object you can use the path field in the AttributePredicate to specify which index\key using an array with each item corresponding to a level, e.g.:

Given a JSON structure like:

{
  "obj1": {
    "obj1Key1": [
       {
          "obj2Key1": true
       }
    ]
  }
}

Then an attribute predicate for an attribute called meter_number to test obj2Key1 would be:

{
  "path": [
    "obj1",
    "obj1Key1",
    0,
    "obj2Key1"
  ],
  "name": {
    "match": "EXACT",
    "value": "meter_number",
    "predicateType": "string"
  },
  "value": {
    "predicateType": "boolean",
    "value": true
  }
}
2 Likes