1

I have written the shell script and getting JSON result from the curl. I want to get the value of key form JSON response without using unix tools like jq, python etc. Thanks in advance.

Rupesh Patil
  • 109
  • 2
  • 10
  • 2
    So, what JSON do you have, what part do you want to extract and how does your current code fail? Please edit your post and show the [mcve]. – Corion Nov 26 '18 at 14:09
  • @Corion Please have a look at the answer I posted and let me know if you have optimal solution. – Rupesh Patil Nov 28 '18 at 07:13

1 Answers1

-1

Got the solution,

I am getting following JSON response from curl and storing in variable :

CURL_OUTPUT='{ "url": "protocol://xyz.net/9999" , "other_key": "other_value" }'

Question :I want to read the url key value and extract the id from that url:

Answer : _ID=$(echo $CURL_OUTPUT |awk '{print $2}' FS='url":' |awk '{print $1}' FS=',' | awk '{print $2}' FS='"'|awk '{print $4}' FS='/')

Rupesh Patil
  • 109
  • 2
  • 10
  • That is a bit of overkill -- 4 calls to `awk` and 4 calls to `printf` within a *command substitution* -- that's a minimum of 9-subshells spawned plus 4 pipes just to parse `'9999'`. Better to make a single call to `grep` assign the result to a temporary variable and then use the shell parameter expansion to delete up to the last `'/'`, e.g. `tmp=$(echo "$CURL_OUTPUT" | grep -o 'protocol[^", ]*'); _ID="${tmp##*/}"` – David C. Rankin Nov 28 '18 at 07:22