Skip to content

Instantly share code, notes, and snippets.

@jps3
Last active October 8, 2019 18:43
Show Gist options
  • Save jps3/2d0252ec859d5c34d6bb0fe8f00b4d5f to your computer and use it in GitHub Desktop.
Save jps3/2d0252ec859d5c34d6bb0fe8f00b4d5f to your computer and use it in GitHub Desktop.
jq: select dicts containing sub-dicts containing a key "b"
{
"a": {
"b": {
"key_1": "value_1"
}
},
"e": {
"b": {
"key_3": "value_3"
}
}
}
{
"a": {
"b": {
"key_1": "value_1"
},
"c": {
"key_1": "value_1"
}
},
"d": {
"a": {
"key_2": "value_2"
},
"c": {
"key_2": "value_2"
}
},
"e": {
"b": {
"key_3": "value_3"
},
"c": {
"key_3": "value_3"
}
}
}
# Given valid JSON input file from colleague ... >:-/
cat example-valid-input.json | jq '[ to_entries[] | select(.value.b) | { key: .key, value: {"b": .value.b} } ] | from_entries'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment