How to query array of objects

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Hereā€™s an outline with best practices for making your inquiry.

My question:
I need to construct a query using XPath to find objects that have a member array with specific key:value.

Details (like screenshots):

I have a REST service that returns the following data:

{
    "content": [
        {
            "domain": "one",
            "tags": [
                {
                    "key": "foo",
                    "value": "bar"
                },
                {
                    "key": "baz",
                    "value": "blah"
                }
            ]
        },
        {
            "domain": "two",
            "tags": [
                {
                    "key": "foo",
                    "value": "bar"
                }
            ]
        },
        {
            "domain": "three",
            "tags": [
                {
                    "key": "baz",
                    "value": "blah"
                }
            ]
        }
    ],
    "totalElements": 3,
    "numberOfElements": 3
}

I can successfully query by ā€œdomainā€ with:

https://{{myServer}}/path/to/service?$filter=domain eq 'one'

What I want to do is query for all of the objects where key = foo and value = bar.

How I found the problem:
Still working on it.

Iā€™ve already tried:

https://{{myServer}}/path/to/service?$filter=tags[foo] eq 'bar'

https://{{myServer}}/path/to/service?$filter=tags['foo'] eq 'bar'

https://{{myServer}}/path/to/service?$filter=tags.[foo] eq 'bar'

https://{{myServer}}/path/to/service?$filter=tags.key[foo] eq 'bar'

@aenagy Welcome to the community :partying_face:

You can try


https://{{myServer}}/path/to/service?$filter=tags[0].key eq 'foo'

Since you are trying to access the array inside in.

This tool will help to locate the elements.

Let me know if this works :slightly_smiling_face:

1 Like

@bpricilla :

Thank-you for your reply.

I see now that my question didnā€™t include the caveat that I couldnā€™t be assured which element within the array would contain the relevant ā€˜keyā€™ and ā€˜valueā€™ properties. I need for the query to find the matching key:value pair regardless of which element it occupies. After doing some more research I (kinda) found my answer on the vendorā€™s web site:

API Tag Filtering Examples

The example " Filter Networks by Cloud Account ID and Environment" most closely matches my use case:

GET /iaas/api/fabric-networks?$filter=cloudAccountIds.item eq 'ec4822a755a755906c6b3822b2' and tags.item.key eq 'environmentā€™ and tags.item.value eq 'dev'

The problem I see with this approach is that if I want to match multiple pairs, e.g. ā€œkey1:value1 AND key2:value2ā€ it doesnā€™t distinguish between ā€œkey1:value1 AND key2:value2ā€ vs ā€œkey1:value2 AND key2:value1ā€. Admittedly this should be a rare case.

BTW, the tool link (https://jsonpathfinder.com/) is handy. Thanks again.