JSONAta get the name of the key containing value

3k Views Asked by At

I am using JSONAta library to pass through a complex object. I need to get the name of the keys if it matches a certain condition.

{
"properties": {
                "WTID": {
                    "pattern": "reference-data",
                    "type": "string"
                },
                "VCRSID": {
                    "pattern": "reference-data",
                    "type": "string"
                },
                "VMSID": {
                    "pattern": "reference-data",
                    "type": "string"
                },
                "DroneID": {
                    "pattern": "unique-data",
                    "type": "string"
                }
            }
}

I want all the name of the keys whose pattern is equal to reference-data i.e. WTID, VCRSID, VMSID. How is it possible to do using JSONAta Query.

So far I have tried using below query:

**[$contains(pattern, 'reference-data')]

but this returns only the values and I am unable to refer to any of the keys.

1

There are 1 best solutions below

0
On

If you're expecting ["WTID", "VCRSID","VMSID"] as a result, then this is the solution:

$keys(properties) ~> $filter(function ($key) {
  $lookup(properties, $key).pattern = "reference-data"
})
  1. Get all the keys of the properties object as an array.
  2. Go through each key, lookup for the value at $key inside properties object.
  3. Return the key only if pattern property from the object from the previous step equals to "reference-data"

You can also play with the interactive version of this solution here.