Search code examples
jsonata

How to return JSONata Sibling attribute


I have some JSON that looks similar to this:

[
    {
        "id": "0oa3nfof0kFRol89o1d7",
        "name": "saasure",
        "label": "Console",
        "status": "ACTIVE",
        "lastUpdated": "2022-06-20T19:34:29.000Z",
        "created": "2022-05-10T22:24:40.000Z",
        "accessibility": {
            "selfService": false,
            "errorRedirectUrl": null,
            "loginRedirectUrl": null
        },
        "visibility": {
            "autoSubmitToolbar": false,
            "hide": [
              "firstName",
              "lastName"
            ],
            "appLinks": {
                "admin": true
            }
        }
    },
    {
        "id": "0oa3nfof2kbmp9TVH1d7",
        "name": "enduser",
        "label": "Dashboard",
        "status": "ACTIVE",
        "lastUpdated": "2022-06-20T19:34:29.000Z",
        "created": "2022-05-10T22:24:42.000Z",
        "accessibility": {
            "selfService": false,
            "errorRedirectUrl": null,
            "loginRedirectUrl": null
        },
        "visibility": {
            "autoSubmitToolbar": false,
             "hide": [
              "lastName"
            ],
            "appLinks": {}
        }   
    }
]

I would like to create a JSONata query the will return only the 'label' attribute of objects where visibility.hide contains "firstName".

Using this query I am able to get the right node in the data.

visibility["firstName" in hide]

but since 'label' is a sibling to 'visibility' it's not pulled. I tried constructing a return object using parent (%)

visibility["firstName" in hide].{
    'label' : %.label
}

But that pulled all the labels. I just want the one label is the sibling of the visibility that matched.


Solution

  • You can filter an array by a deeply nested property comparison: $["firstName" in visibility.hide].label

    See playground solution here: https://stedi.link/iDqghJb