Decode JSON in Bash using python mjson.tool

15,332

Solution 1

$ echo '{"first_key": "value", "second_key": "value2"}' | python -c 'import sys, json; print(json.load(sys.stdin)[sys.argv[1]])' first_key
value

Solution 2

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
Share:
15,332
Justin
Author by

Justin

Updated on June 11, 2022

Comments

  • Justin
    Justin almost 2 years

    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.