Home > Back-end >  Parse a value from the json
Parse a value from the json

Time:11-26

I have a following json

{
"kind":"testObject",
"spec":{
},
"status":{
"code":"503"
  }
}

I would like to just retrieve the value of code, so that it would just show "503" as an output.

I did try with jmespath, but that binary is out of question, and not sure what to use.

CodePudding user response:

Access the value with .status.code:

$ jq '.status.code' <<JSON
{
 "kind":"testObject",
 "spec":{
 },
 "status":{
  "code":"503"
 }
}
JSON
"503"

If you want your output to be 503 (as compared to "503"), use --raw-output/-r:

$ jq -r '.status.code' <<JSON
{
 "kind":"testObject",
 "spec":{
 },
 "status":{
  "code":"503"
 }
}
JSON
503

CodePudding user response:

Since this question has the tag, it is worth pointing out that one could use the JMESPATH command-line tool, jp, as follows:

jp status.code

or to suppress the quotation marks:

jp -u status.code

Similarly, using jaq, which has a jq-based syntax, one could write:

jaq .status.code

or to suppress the quotation marks:

jaq -r .status.code
  • Related