1

is there any way to use object.get with multiple level key..?

My input looks like this: { "pipelineParameters" : { "k8" : { "NODES" : "1" }, "ec2": { "NODES" : "0" } }

my data looks like { "key": "pipelineParameters.k8.NODES" }

How can I get value from input based on multilevel key

Sample code

https://play.openpolicyagent.org/p/iR15XnMctP

Jonas
  • 121,568
  • 97
  • 310
  • 388
Nuthan Kumar
  • 483
  • 5
  • 22

1 Answers1

2

The object.get function does not support multi-level keys. You could use the walk function for this if you represent the key as an array:

input = {
    "pipelineParameters" : {
        "k8" : {
            "NODES" : "1"
        },
        "ec2": {
           "NODES" : "0"
        }
    }
}

For example:

> walk(input, [["pipelineParameters", "k8", "NODES"], "1"])
true
> walk(input, [["pipelineParameters", "k8",  "NODES"], x])
+-----+
|  x  |
+-----+
| "1" |
+-----+
> walk(input, [["pipelineParameters", y,  "NODES"], x])
+-----+-------+
|  x  |   y   |
+-----+-------+
| "1" | "k8"  |
| "0" | "ec2" |
+-----+-------+

To convert your key into array you could simply write:

split(key, ".")

For example:

split("pipelineParameters.k8.NODES", ".")
[
  "pipelineParameters",
  "k8",
  "NODES"
]

Putting it all together:

> walk(input, [split("pipelineParameters.k8.NODES", "."), x])
+-----+
|  x  |
+-----+
| "1" |
+-----+
tsandall
  • 1,544
  • 8
  • 8