0

I am trying to use jmespath for my querying my json and I am trying to access a property which is at a higher level.

Below is my JSON:

{
    "properties": {
        "DefaultVMTypeID": "RT",
        "VM": {
            "measurements": [
                {
                    "vm": 45.62,
                    "vmString": "45.62",
                    "vmID": "RT",
                    "vmPathID": "osdu",
                    "vmTypeID": "RT",
                    "vmUnitOfMeasureID": "m"
                },
                {
                    "vm": 65,
                    "vmString": "65",
                    "vmID": "MT",
                    "vmPathID": "sample",
                    "vmTypeID": "MT",
                    "vmUnitOfMeasureID": "m"
                },
                {
                    "vm": 32,
                    "vmString": "32",
                    "vmID": "MT",
                    "vmPathID": "osduschemas",
                    "vmTypeID": "MT",
                    "vmUnitOfMeasureID": "m"
                },
                {
                    "vm": 95,
                    "vmString": "95",
                    "vmID": "MT",
                    "vmPathID": "schema",
                    "vmTypeID": "MT"
                }
            ]
        }
    }
}

I want to get all the measurements whose vmId is equal to DefaultVMTypeID.

I tried below query: [properties.DefaultVerticalmeasurementTypeID, properties.Verticalmeasurements.measurements[?VerticalmeasurementTypeID]] | map(&[1].VerticalmeasurementTypeID==@[0], @)

but when applying map to the array @ refers to the element of the array and inside map there is no way I an access DefaultVMTypeID.

I have also tried transforming each element of an object.

Any leads would be appreciated.

Ankit Tanna
  • 1,779
  • 8
  • 32
  • 59

2 Answers2

0

I came across the TS translation on JMESPath library which has 2 enhancements.

  • Custom functions
  • Root Value Access

https://github.com/nanoporetech/jmespath-ts

This helped me solve my problem. [properties.Verticalmeasurements.measurements][] | [?VerticalmeasurementID==$.properties.DefaultVerticalmeasurementTypeID]

Ankit Tanna
  • 1,779
  • 8
  • 32
  • 59
0

You can actually use $ inside expressions to access the root of the JSON.

So the JSON Path

$.properties.VM.measurements[?(@.vmID==$.properties.DefaultVMTypeID)]

should work to get all the items in the measurements array that match.

gregsdennis
  • 7,218
  • 3
  • 38
  • 71