I need to get a key
from JSON in standard bash, and found the following:
echo '{"first_key": "value", "second_key": "value2"}' | python -mjson.tool | grep 'first_key'
But this returns:
"first_key": "value",
How can I just return value
, i.e. not the key, and remove the quotes and comma.
Thanks.
$ echo '{"first_key": "value", "second_key": "value2"}' | python -c 'import sys, json; print(json.load(sys.stdin)[sys.argv[1]])' first_key
value
Since you tagged it grep
here is a solution for that (though Ignacio's solution is the right way to do it):
echo "..." | grep -oP "(?<=\"first_key\": \")[^\"]+"
Output:
$ echo '{"first_key": "value", "second_key": "value2"}' | grep -oP "(?<=\"first_key\": \")[^\"]+"
value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With