Skip to content

Instantly share code, notes, and snippets.

@ajorpheus
Last active May 11, 2023 15:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajorpheus/ae445bd4007eafc77e6de6730a6f7a32 to your computer and use it in GitHub Desktop.
Save ajorpheus/ae445bd4007eafc77e6de6730a6f7a32 to your computer and use it in GitHub Desktop.
JQ: Case-insensitive search for keys containing string

Search for keys in JSON at any level containing the supplied string and strips out any empty results

cat some-json |  jq -c '.. | objects | with_entries(select(.key |match("SEARCH-STRING";"i"))) | select(. != {})'

For instance, to list any keys that contain the word public from a list of AWS instances :

aws ec2 describe-instances --output json | jq -c '.. | objects | with_entries(select(.key |match("public";"i"))) | select(. != {})'

Outputs:

{"PublicDnsName":"xxxxx.compute-1.amazonaws.com","PublicIpAddress":"xx.xx.xx.xx"}
{"PublicDnsName":"xxxxx.compute-1.amazonaws.com","PublicIp":"xx.xx.xx.xx"}
{"PublicDnsName":"xxxxx.amazonaws.com","PublicIp":"xx.xx.xx.xx"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment