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
jq 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
Related videos on Youtube
Author by
op32
Updated on September 18, 2022Comments
-
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 over 7 yearsYes,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 over 7 yearsWhat is the response if you use
echo "$uuid"
instead ofecho $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 over 7 yearsAnother 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 over 7 yearsSince that is a new operation it is probably better to ask another question.
-
they over 2 yearsWithout the extra fluff:
curl ... | tee final.txt | jq ...
, or forget thetee
if it's not actually needed (unclear in question).