I'm using bash shell. I would like to include something in my shell script that can extract the value of a certain key in a JSON string ...
davea$ json='{"id": "abc", "name": "dave"}'
I tried "grep", which failed
davea$ grep -Po '"id":.*?[^\\]",' $json
usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
Then I found a solution involving Python, but this also failed ...
localhost:tmp davea$ echo $json | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["id"]'
File "<string>", line 1
import json,sys;obj=json.load(sys.stdin);print obj["id"]
How can I extract the value for the "id" key without installing anything extra on my system?
jqorjsonor don't work with JSON data.printargument.bashshell, but your examples usesgrepandpython. Would it be ok to update the title?