0

in the bash script I'm running a curl for POST request getting data

f_curl_get_data (){

read -p "start date : " start_date
read -p "end date : " end_date

# (!) NOTE 
# - the date time format must be YYYY-MM-DD

mng_type=users
user=myuser
secret=mysecret

curl --location --request POST 'https://myapi.com/api/v2.1/rest/exports' \
    --header 'Content-Type: application/json' \
    --header 'SDK-APP-ID: '$user'' \
    --header 'SDK-SECRET: '$secret'' \
    --data-raw '{
        "type":"'$mng_type'",
        "start_date":"'$start_date'",
        "end_date": "'$end_date'"
    }' 

}

and I get the following results

{"results":{"created_at":"2020-03-13T07:04:14Z","download_url":"","error_message":"","original_filename":"2020-03-13T07:04:14Z_exported_users.json","percentage":0,"resource_name":"users","size":0,"status":"started","total_rows":0,"unique_id":"37c23e60-5b83-404a-bd1f-6733ef04463b"},"status":200}

how do I just get the value from the variable "unique_id" with awk command or other?

37c23e60-5b83-404a-bd1f-6733ef04463b

thank u

1
  • 1
    Better to use jq since this is JSON data. Commented Mar 13, 2020 at 7:53

2 Answers 2

1

Using sed

sed -e 's/.*unique_id":"\(.*\)\"}.*/\1/'

Demo :

:>echo '{"results":{"created_at":"2020-03-13T07:04:14Z","download_url":"","error_message":"","original_filename":"2020-03-13T07:04:14Z_exported_users.json","percentage":0,"resource_name":"users","size":0,"status":"started","total_rows":0,"unique_id":"37c23e60-5b83-404a-bd1f-6733ef04463b"},"status":200}' | sed -e 's/.*unique_id":"\(.*\)\"}.*/\1/'
37c23e60-5b83-404a-bd1f-6733ef04463b


Sign up to request clarification or add additional context in comments.

Comments

1

Using GNU awk and json extension:

$ gawk '
@load "json"                                # load extension
{
    lines=lines $0                          # in case of multiline json file
    if(json_fromJSON(lines,data)!=0) {      # explode valid json to an array
        print data["results"]["unique_id"]  # print the object value
        lines=""                            # in case there is more json left
    }
}' file

Output:

37c23e60-5b83-404a-bd1f-6733ef04463b

Extension can be found in there:

http://gawkextlib.sourceforge.net/json/json.html

... or you could use jq:

$ jq -r '.results.unique_id' file
37c23e60-5b83-404a-bd1f-6733ef04463b

1 Comment

thank you for providing a solution, very appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.