honzito
honzito

Reputation: 1

Parse a value from the json

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.

Upvotes: 0

Views: 65

Answers (2)

peak
peak

Reputation: 116640

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

Upvotes: 1

knittl
knittl

Reputation: 265131

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

Upvotes: 1

Related Questions