Last active
August 29, 2015 14:11
-
-
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
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
# 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