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 jmespath 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