Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Value from a curl command output

Tags:

json

curl

I have below 2 format for output which is an output of curl command the 1st one is from the swagger site and the 2nd is when I run the curl from linux.

I want to get the value for data fields in both the cases like the current value of data is 0 so it should be coming as 0 without double quotes.

how can we extract it in linux :

{
  "status": "SUCCESS",
  "responseData": {
    "data": [
      "0"
    ]
  },
  "error": null
}

OR

{"status":"SUCCESS","responseData":{"data":["0"]},"error":null}
like image 813
MoshinK Avatar asked Aug 24 '26 06:08

MoshinK


1 Answers

You can use for that e.g. jq command with -r (or --raw-output) flag as follows: (since the value of data is an array get e.g. the first element with [0])

curl https://server.com/rest-api/foo | jq -r '.responseData.data[0]'

# Test:
echo '{"status":"SUCCESS","responseData":{"data":["0"]},"error":null}'| jq -r '.responseData.data[0]'
# Output:
0
like image 55
1c0DevPi Avatar answered Aug 25 '26 22:08

1c0DevPi