7

I am writing a bash script which has a json value stored in a variable now i want to extract the values in that json using Jq. The code used is.

json_val={"code":"lyz1To6ZTWClDHSiaeXyxg","redirect_to":"http://example.com/client-redirect-uri?code=lyz1To6ZTWClDHSiaeXyxg"}
  code_val= echo"$json_val" | jq '.code'

This throws an error of no such file or direcotry.

If i change this to

json_val={"code":"lyz1To6ZTWClDHSiaeXyxg","redirect_to":"http://example.com/client-redirect-uri?code=lyz1To6ZTWClDHSiaeXyxg"}
  code_val=echo" $json_val " | jq '.code'

This does not throws any error but the value in code_val is null.

If try to do it manually echo {"code":"lyz1To6ZTWClDHSiaeXyxg","redirect_to":"http://example.com/client-redirect-uri?code=lyz1To6ZTWClDHSiaeXyxg"} | jq '.code' it throws parse numeric letter error.

how can i do it in first case.

2 Answers 2

15

You may use this:

json_val='{"code":"lyz1To6ZTWClDHSiaeXyxg","redirect_to":"http://example.com/client-redirect-uri?code=lyz1To6ZTWClDHSiaeXyxg"}'
code_val=$(jq -r '.code' <<< "$json_val")
echo "$code_val"

lyz1To6ZTWClDHSiaeXyxg

Note following changes:

  • Wrap complete json string in single quotes
  • use of $(...) for command substitution
  • Use of <<< (here-string) to avoid a sub-shell creation

PS: If you're getting json text from a curl command and want to store multiple fields in shell variables then use:

read -r code_val redirect_to < <(curl ... | jq -r '.code + "\t" + .redirect_to')

Where ... is your curl command.

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

4 Comments

in this you have put the json response in ''(single quotes) which makes changes it to literal string. I cannot do this coz i am getting the json_val as a curl response after i run the curl command in earlier line and i save the response in that variable. Or please suggest a way to convert the data in variable to string and then pass to jq.
yeah but there are multiple fields which i need to extract and save in two different variable from the same curl response. Can you suggest a way to extract two values and save in two diff variable from the curl json response. That would do my work.
it does work but also throws errors of echoset not defined read -r code_val redirect_to < <(curl 5dba6fe9eddc81001495f72c.mockapi.io/tyk_test/rate_ poc | jq -r '.rate + "\t" + .individual') echo"$code_val" echo"$redirect_to"
Run curl with my suggested ka command on shell and see if that runs fine
1

If try to do it manually:

$ echo {"code":"lyz1To6ZTWClDHSiaeXyxg","redirect_to":"http://example.com/client-redirect-uri?code=lyz1To6ZTWClDHSiaeXyxg"} | jq '.code'

...it throws parse numeric letter error.

seems like you did not escape the string of the echo command. in your case, escaping with a singe-quote (apostrophe ') will do - same as you did with the jq json-path argument ('.code')

$ echo '{"code":"lyz1To6ZTWClDHSiaeXyxg","redirect_to":"http://example.com/client-redirect-uri?code=lyz1To6ZTWClDHSiaeXyxg"}' | jq '.code'
"lyz1To6ZTWClDHSiaeXyxg"

Comments

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.