Search code examples
jsonata

How to cross reference attributes with JSON Query


Consider following JSON.

I want to get "Measurement" such that "measurementID" is equal to value of "mid" node.

 {
"myroot": {
    "name": "ABC",
    "nameAliases": [
        {
            "AliasName": "23123",
            "AliasNameTypeID": "test"
        }
    ],
    "nameTypeID": "test",
    "mid": "RR",
    "measurements": [
        {
            "Measurement": 2.62,  //-> This should be output of the JSON query
            "measurementID": "RR",
            "MeasurementPath": "demo",
            "measurementType": "TT",
            "UnitOfMeasure": "m"
        },
        
        {
            "Measurement": 40.62,
            "measurementID": "TR",
            "MeasurementPath": "demo",
            "measurementType": "T",
            "measurementUnitOfMeasure": "m"
        }
    ]
}  

This query works but it has hardcoded value 'RR' $.myroot.measurements[measurementID = "RR"]

I want to refer to other attribute inside the query. I tried following query but it did not work

$.myroot.measurements[measurementID = $.**.mid]

Can someone please tell me what is the correct query.

Atul


Solution

  • Within your filter expression, you need to use $$ to reference your mid property relative to the root of the document. Using $ it will try and find it relative to the measurements array.

    myroot.measurements[measurementID = $$.myroot.mid].Measurement
    

    See https://try.jsonata.org/pf5pt9MMj