4

I have the following input:

{
  "key1": {
    "subkey1": [
      {
        "filterkey1": "value1",
        "filterkey2": "value2"
      },
      {
        "filterkey1": "value3",
        "filterkey2": "value4"
      }
    ],
    "subkey2": [
      {
        "filterkey1": "value5",
        "filterkey2": "value6"
      },
      {
        "filterkey1": "value7",
        "filterkey2": "value8"
      }
    ],
    "subkey3": [
      {
        "filterkey1": "value1",
        "filterkey2": "value6"
      },
      {
        "filterkey1": "value9",
        "filterkey2": "value4"
      }
    ]
  },
  "key2": {
  }
}

I want to get the key of the arrays that has an object which has "value1" for key: "filterkey1". So in this case the output must be:

["subkey1", "subkey3"]

All the elements I care about are in "key1" object.

1 Answer 1

4

Get subkeys using keys_unsorted, and check if their values have filterkey1: "value1" pair using any:

.key1 | [
    keys_unsorted[] as $k
    | if any(.[$k][]; .filterkey1=="value1")
    then $k 
    else empty end
]
Sign up to request clarification or add additional context in comments.

Comments

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.