1

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?

  • 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. – op32 Nov 22 '16 at 15:17
  • 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...) – roaima Nov 22 '16 at 15:35
2

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
  • 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) – op32 Nov 22 '16 at 16:48
  • Since that is a new operation it is probably better to ask another question. – maulinglawns Nov 22 '16 at 16:53
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.