0

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

chinnu Nish
  • 119
  • 1
  • 13

1 Answers1

2

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.

Simon
  • 4,251
  • 2
  • 24
  • 34
  • I dont have jq installed in my client machine. Hence tried for grep command. Any other option to extract that node value – chinnu Nish Sep 12 '20 at 10:09
  • Extracting a nested value from JSON using just UNIX tools might be tricky, I have added another example using `python` and a link for further reading – Simon Sep 12 '20 at 10:23