Last active
January 22, 2024 13:02
-
-
Save pedroxs/f0ee8c515eea0dbce2e23eea7c048e10 to your computer and use it in GitHub Desktop.
jq - recursive search for keys containing "string" stripping empty results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# recursive search for keys containing "string" stripping empty results | |
jq '.. | objects | with_entries(select(.key | contains("ftp"))) | select(. != {})' | |
# same, but output propper array | |
jq '[ .. | objects | with_entries(select(.key | contains("ftp"))) | select(. != {}) ]' | |
# or | |
jq 'map( .. | objects | with_entries(select(.key | contains("ftp"))) | select(. != {}) )' | |
# transform input from {type: a, amount: 1} to {a: 1} and sum all values by type | |
jq '[ .[] | {(.type): .amount} ] | map(to_entries) | add | group_by(.key) | map({key: .[0].key, value: map(.value) | add}) | from_entries' |
To have a proper array
as result output surround everything with [
and ]
, example:
jq '[ .. | objects | with_entries(select(.key | contains("ftp"))) | select(. != {}) ]'
Wow! It's so amazing, thx guys!
But how use new array correctly?
In this example:
{
"ImageId": "ami-fce3c696"
}
{
"ImageId": "ami-0d72bbaa068279155"
}
How I can get every ImageId
value? Or get plain text like that for use that in bash-scripting?
ami-fce3c696
ami-0d72bbaa068279155
I need use another jq in pipe or we have another way?
@ksemele to get raw text use -r
modifier. And to get a value just filter them by their key name, example:
❯ echo '{
"ImageId": "ami-fce3c696"
}
{
"ImageId": "ami-0d72bbaa068279155"
}' | jq '.ImageId' -r
ami-fce3c696
ami-0d72bbaa068279155
thanks! This is super useful. When I only care about one key this lets me avoid having to figure out the exact complicated structure of the big object being returned. Cheers!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a great start! JQ is pretty powerful but arcane. In my case, this produces a series of objects that are not valid json:
I know you can get it to fix this somewhere with the correct
map