0

I am not able to debug why it is returning

null
value. Can some-one please help into this.

Code -

getJSONParamater() {
       echo "Before : $1 $2 $3"
       eval "$3=$(cat $1 | jq '."$2"')"
       echo "After : $1 $2 $3"
}

return_value='default'
getJSONParamater etl-without-transformation.json  success_email return_value

echo $return_value
4
  • @Inian - I just tried it and it works for me with double quotes Commented Jun 10, 2016 at 9:04
  • @devsda presumably its because cat ... | jq ... command is returning null Commented Jun 10, 2016 at 9:07
  • 1
    To debug a shell script, put set -x at the top. Then it will display each line as it executes it, so you can see what's going wrong. Commented Jun 10, 2016 at 9:11
  • At the very least, use declare instead of eval (although there are even better alternatives) to protect against (some) unintended code evaluation. An extreme example is getJSONParameter f.json value ":; echo hi; name", which would echo "hi" before setting name equal to the result of the command. Commented Jun 10, 2016 at 13:06

2 Answers 2

4

jq '."$2"' will send literal ."$2" as a expression for jq, what you want is:

jq --arg key "$2" '.[$key]' "$1"

Also removed useless use of cat.

I don't quite understand what you want with the eval part? But I guess you are trying to set return_value to the result from jq?

getJSONParameter() {
  jq --arg key "$2" '.[$key]' "$1"
}

return_value="$(getJSONParameter "etl-without-transformation.json" "success_email")"
Sign up to request clarification or add additional context in comments.

Comments

1

Another way to do it by using global variable:

return_value='default'
getJSONParamater() {
       return_value=$(cat "$1" | jq "$2")
}
getJSONParamater etl-without-transformation.json  .success_email
echo $return_value

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.