Create list from array for Evaluation #157
-
Hi! Have a fun Rego question for the community. I am trying to evaluate a list of JSON array to confirm a specific object has a requited name and haven't figured out how to grab it successfully yet. I was wondering how it might be possible to make a list of the values from the array and then evaluate that list to see if the name exists. The array looks roughly like this
In this case I would be wanting to confirm the resource has a data array with the name The above example should pass and the below should fail.
thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
If you want to aggregate an array of the resource names, you could either use an array comprehension, like: import future.keywords.in
obj := {"data": [
{
"name": "resource",
"resource": {"data": {"name": "good-eval-name"}},
},
{
"name": "resource",
"resource": {"data": {"name": "bad-eval-name"}},
},
]}
resource_names := [name | some item in obj.data; name := item.resource.data.name] Or you could use a partial rule to build a set of them: resource_names[name] {
some item in obj.data
name := item.resource.data.name
} (You could also use a set comprehension to achieve the same outcome, they are functionally equivalent) Then simply use not "bad-eval-name" in resource_names I'd probably choose to work with this as a set, since ordering doesn't seem to matter (and neither uniqueness). Let me know if that answers your question or if I should elaborate on something :) |
Beta Was this translation helpful? Give feedback.
If you want to aggregate an array of the resource names, you could either use an array comprehension, like:
Or you could use a partial rule to build a set of them:
(You could also use a set comprehension to achieve the same outcome, they are functionally equivalent)
Then simply use
in
to check for membership: