Monday, November 1, 2021

[SOLVED] Decode JSON in Bash using python mjson.tool

Issue

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.


Solution

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


Answered By - Ignacio Vazquez-Abrams