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

1 Answer 1

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" |
+-----+
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I did not check 18.0 documentation. It worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.