Skip to content

Instantly share code, notes, and snippets.

@pedroxs
Last active January 22, 2024 13:02
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pedroxs/f0ee8c515eea0dbce2e23eea7c048e10 to your computer and use it in GitHub Desktop.
Save pedroxs/f0ee8c515eea0dbce2e23eea7c048e10 to your computer and use it in GitHub Desktop.
jq - recursive search for keys containing "string" stripping empty results
# 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'
@chb0github
Copy link

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:

{
  "ImageId": "ami-fce3c696"
}
{
  "ImageId": "ami-0d72bbaa068279155"
}

I know you can get it to fix this somewhere with the correct map

@pedroxs
Copy link
Author

pedroxs commented May 2, 2019

@chb0github

To have a proper array as result output surround everything with [ and ], example:
jq '[ .. | objects | with_entries(select(.key | contains("ftp"))) | select(. != {}) ]'

@ksemele
Copy link

ksemele commented Apr 15, 2022

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?

@pedroxs
Copy link
Author

pedroxs commented May 26, 2022

@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

@varenc
Copy link

varenc commented Nov 23, 2022

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