Extract UUID value from JSON returned from HTTP reply

6,606

Solution 1

Now i need to extract the value after "serviceUuid"

So, if the variable $uuid contains this:

echo "$uuid"
{"status":{"code":"STATUS_OK","message":"ServiceUUID sent successfully via..."},"body":{"data":{"userApps":{},"username":"HereTheUsername","fullName":"NameOfTheAccountPossessor","lang":"sq","blocked":false,"lastLogin":"2016-10-10T17:19:22","passwordResetUuid":"6147dc32-b72e-450a-8084-2fdb5319a931","userAccessLevel":5,"countDownSeconds":0,"serviceUuid":"7260276c-5c3f-41d3-9329-3603acecb7e5","userAttributes":{},"labelMap":{},"id":"APPUSER00000012","someLabel":"NameOfTheOrganisation"}}}

...and you only want the serviceUuid value, I would do this:

echo "$uuid" | sed -nE 's/.*"serviceUuid":"(.*)","user.*/\1/p'
7260276c-5c3f-41d3-9329-3603acecb7e5

In your case, it would look like:

echo "$uuid" | sed -nE 's/.*"serviceUuid":"(.*)","user.*/\1/p' >> final.txt

...to append it to the file final.txt


sed --version
sed (GNU sed) 4.2.2

Solution 2

is a JSON parsing tool. You can do this:

uuid=$(curl ...)
service_uuid=$(jq -r '.body.data.serviceUuid' <<<"$uuid")
echo "$service_uuid"
7260276c-5c3f-41d3-9329-3603acecb7e5
Share:
6,606

Related videos on Youtube

op32
Author by

op32

Updated on September 18, 2022

Comments

  • op32
    op32 over 1 year

    I am trying to make a little script in shellscript that takes one serviceUuid from a response and use it. I just print the server response on a file called final.txt Now i need to extract the value after "serviceUuid":"

    This is the script:

    uuid=$(curl   -X POST -H "ACCEPT-LANGUAGE:en"   -H "Content-Type: application/json"   -H "Accept: application/json" -d  {"username":"HereThereIsTheUsername"}  Here there is the url )
    
    echo $uuid >> final.txt
    

    this is the response:

    {"status":{"code":"STATUS_OK","message":"ServiceUUID sent successfully via..."},"body":{"data":{"userApps":{},"username":"HereTheUsername","fullName":"NameOfTheAccountPossessor","lang":"sq","blocked":false,"lastLogin":"2016-10-10T17:19:22","passwordResetUuid":"6147dc32-b72e-450a-8084-2fdb5319a931","userAccessLevel":5,"countDownSeconds":0,"serviceUuid":"7260276c-5c3f-41d3-9329-3603acecb7e5","userAttributes":{},"labelMap":{},"id":"APPUSER00000012","someLabel":"NameOfTheOrganisation"}}}
    

    So can someone help me to extract the value?

    • op32
      op32 over 7 years
      Yes,i don't give the url,username or name of the guy so just place everything you want on the fields that i changed.
    • roaima
      roaima over 7 years
      What is the response if you use echo "$uuid" instead of echo $uuid? The whitespace is important if you're going to try and parse JSON from shell. (Remember to edit this into your question and format it with the {} button...)
  • op32
    op32 over 7 years
    Another little question about this...now i need to use the value extracted (serviceUuid) and to paste it on the script below: login=$(curl -X POST -H "ACCEPT-LANGUAGE:en" -H "Content-Type: application/json;charset=utf-8" -H "Accept-Encoding:gzip" -H "Accept: application/json" -d {"serviceUuid":"Here"} ServerUrl) (it's everything inside the file aass.sh)
  • maulinglawns
    maulinglawns over 7 years
    Since that is a new operation it is probably better to ask another question.
  • they
    they over 2 years
    Without the extra fluff: curl ... | tee final.txt | jq ..., or forget the tee if it's not actually needed (unclear in question).