I have the following json response
{"data":{"serverPort":0,"runId":7008,"runAction":false,"runStatus":"started"},"total":1,"success":true}
Wanted to retrieve the value of runStatus. How can i do this using grep command
I have the following json response
{"data":{"serverPort":0,"runId":7008,"runAction":false,"runStatus":"started"},"total":1,"success":true}
Wanted to retrieve the value of runStatus. How can i do this using grep command
Mangling JSON with grep
may not be the right way to go. To work with JSON on the command line, I would recommend to use the jq
utility (https://stedolan.github.io/jq/):
$ echo '{"data":{"serverPort":0,"runId":7008,"runAction":false,"runStatus":"started"},"total":1,"success":true}' | jq '.data.runStatus'
"started"
Other solutions have been discussed here: Parsing JSON with Unix tools. For example, you could use python
if that is available to you:
$ echo '{"data":{"serverPort":0,"runId":7008,"runAction":false,"runStatus":"started"},"total":1,"success":true}' | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['runStatus'])"
started
However, extracting a nested value from JSON using just UNIX tools might be tricky.