Home > database >  Get Value from a curl command output
Get Value from a curl command output

Time:10-12

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}

CodePudding user response:

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

CodePudding user response:

or you try with sed - assuming format is consistent, something like this

curl https://server.com/rest-api/foo | sed -r 's/^.*data\":\[\"(.*)\"].*$/\1/'
  • Related