0

I have a curl command that gets a JSON response and it gives the response below. I need to get the value of data.value

How can I do this without a "hacky" solution?

{
    "request_id":"50aaabe7-d01b-0a83-da86-8f01cb1da74b",
    "lease_id":"",
    "renewable":false,
    "lease_duration":2764800,
    "data":{"value":"randomBinaryString"},
    "wrap_info":null,
    "warnings":null,
    "auth":null
}

1 Answers1

0

If you install jq, then it is as easy as:

<curl-command> | jq .data.value

If you don't want to install extra software and the response is always in that format, you can do some dirty tricks:

<curl-command> | grep data | tr '":{' '   ' | awk '{print $3}'
Poshi
  • 5,332
  • 3
  • 15
  • 32
  • 1
    Do note the entry for questions which "have already been asked and answered many times before" in the "Answer Well-Asked Questions" section of [How to Answer](https://stackoverflow.com/help/how-to-answer). – Charles Duffy Jul 12 '18 at 19:27
  • 1
    Leaving in the hackish section (when the OP explicitly asks for a non-hacky solution) is... maybe not ideal. If you *were* going to do an ugly hack that's extremely dependent on formatting and data contents and broken in most cases, though, there's little reason for all three of `grep`, `tr` and `awk`; awk alone can do the work of all three. `awk -F'[":{[:space:]]+' '/"data":/ { print $3 }'`, for example (ugh, I feel dirty typing that). – Charles Duffy Jul 12 '18 at 19:28
  • Completely agree. It is just that It was easier for me to think in other terms instead of the field separator string. As you said, that part was the backup (and non requested), the good answer is the first part. BTW, "hacky" can have lots of meanings. My answer uses less special characters than yours, so don't appear to be as much hacky as yours (despite is the same) XD – Poshi Jul 12 '18 at 20:02