Query asset having JSON attribute

Continuing the discussion from How to use AssetQuery to query assets via HTTP API:

…I am trying to query an asset via HTTP query POST.

Asset has the attribute e.g. meter_number (as described in related post)

and its value is

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

So I send HTTP POST to {{baseUrl}}/asset/query with body

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

but as result I get all assets, not only the one which satisfys search criteria.

I tried also this query

{
    "attributes": {
        "items": [
            {
                "path": [
                    "obj1",
                    "obj1Key1",
                    0,
                    "obj2Key1"
                ],
                "name": {
                    "match": "EXACT",
                    "caseSensitive": "true",
                    "value": "meter_number",
                    "negate": "false",
                    "predicateType": "string"
                },
                "value": {
                    "predicateType": "boolean",
                    "value": true
                }
            }
        ]
    }
}

but I get exception

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `org.openremote.model.query.filter.NameValuePredicate$Path` from Array value (token `JsonToken.START_ARRAY`)
 at [Source: (io.undertow.servlet.spec.ServletInputStreamImpl); line: 5, column: 25] (through reference chain: org.openremote.model.query.AssetQuery["attributes"]->org.openremote.model.query.LogicGroup["items"]->java.util.ArrayList[0]->org.openremote.model.query.filter.AttributePredicate["path"])
	at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)

Hi!

The last AssetQuery example you are giving should be the correct format.
At least, that is what I’m used to myself.

I’ve never filtered attributes by path, but I did some digging in the documentation;
Seems that the path field in attributes → items[] is not an array, but an object.

Not 100% sure, but maybe try this;

"path": {
    "paths": [
        "obj1",
        "obj1Key1",
        0,
        "obj2Key1"
    ]
}

HI Martin, thanks for your suggestion!

I have tested as you proposed and this worked. Also made negative test, worked as well.

Here my request

{
    "attributes": {
        "items": [
            {
                "path": {
                    "paths": [
                        "aa",
                        "bb"
                    ]
                },
                "name": {
                    "match": "EXACT",
                    "caseSensitive": "true",
                    "value": "notes3",
                    "negate": "false",
                    "predicateType": "string"
                },
                "value": {
                    "predicateType": "string",
                    "value": "cc"
                }
            }
        ]
    }
}

and here the response

[
    {
        "id": "7QbV4MfKe0egrdcYJZQlLF",
        "version": 4,
        "createdOn": 1704744486849,
        "name": "asset with notes2",
        "accessPublicRead": false,
        "realm": "master",
        "type": "ThingAsset",
        "path": [
            "7QbV4MfKe0egrdcYJZQlLF"
        ],
        "attributes": {
            "notes": {
                "name": "notes",
                "type": "text",
                "meta": {},
                "value": "tt",
                "timestamp": 1704744372267
            },
            "notes3": {
                "name": "notes3",
                "type": "JSON",
                "meta": {},
                "value": {
                    "aa": {
                        "bb": "cc"
                    }
                },
                "timestamp": 1704749313051
            },
            "notes2": {
                "name": "notes2",
                "type": "text",
                "meta": {},
                "value": "aaa",
                "timestamp": 1704744735786
            },
            "location": {
                "name": "location",
                "type": "GEO_JSONPoint",
                "meta": {},
                "value": null,
                "timestamp": 1704744241001
            },
            "meter_number": {
                "name": "meter_number",
                "type": "JSON",
                "meta": {},
                "value": {
                    "obj1": {
                        "obj1Key1": [
                            {
                                "obj2Key1": true
                            }
                        ]
                    }
                },
                "timestamp": 1704752540356
            }
        }
    }
]
2 Likes