Skip to content

Instantly share code, notes, and snippets.

@deltheil
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deltheil/240b4cc68651204e7ef7 to your computer and use it in GitHub Desktop.
Save deltheil/240b4cc68651204e7ef7 to your computer and use it in GitHub Desktop.
Use jq command-line JSON processor to filter out a nested hash. See also: https://github.com/stedolan/jq/wiki/jq%20Cookbook
# http://stedolan.github.io/jq/
json='{"foo": {"genre":"deep house"}, "bar": {"genre": "progressive house"}, "baz": {"genre": "dubstep"}}'
echo "$json" | jq '[to_entries | .[] | select(.value.genre | contains("house"))] | from_entries'
# {
# "bar": {
# "genre": "progressive house"
# },
# "foo": {
# "genre": "deep house"
# }
# }
# To keep only N elements (2 here)
echo "$json" | jq 'to_entries | .[0:2] | from_entries'
# {
# "bar": {
# "genre": "progressive house"
# },
# "baz": {
# "genre": "dubstep"
# }
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment