1

The data is

data_case_c = {
    "qualities": {
        "id": "0123456789",
        "key_criteria": "Target",
        "desired_value": {"id": "987654321", "label": "CORRECT"},
    }
}

I want check that key_criteria is Target. And if it is, return qualities.desired_value.label, which is CORRECT in this case.

Here is the query I thought might work

query = "qualities.key_criteria == 'Target' | qualities.desired_value.label"

It returns None.

The first half of the expression returns True.

query = "qualities.key_criteria == 'Target'"

How can I return the actual value of qualities.desired_value.label when a key on the same level as desired_value matches a specific criteria?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
sam
  • 653
  • 9
  • 21

1 Answers1

1

In order to filter, in JMESPath, you do need a JSON array, which you can achieve on an object with the to_array function

to_array(qualities)

Then you have to throw your filter projection in:

to_array(qualities)[?key_criteria == `Target`]

Select the attribute you want to query:

to_array(qualities)[?key_criteria == `Target`].desired_value.label

And finally, get rid of the array you created to be able to filter, with a pipe expression:

to_array(qualities)[?key_criteria == `Target`].desired_value.label | [0]

And with all this, on your example JSON, you end up with:

"CORRECT"
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83