0

I am trying to use curl to submit a request that requires one of the parameters to be an integer. I want to be able to use a variable for that integer, but can't figure out how to do it without it showing up as a string or failing to substitute the variable in.

stringVar="L"
intVar=4

JSON=\''{"stringVar": "'"$stringVar"'","intVar": "'"$intVar"'" }'\'


printf '%s\n' "$JSON"

I have tried all the combinations I can think of of quotes no quotes and braces and can't figure it out.

Corey
  • 170
  • 2
  • 11
  • Can you post the desired result? – accdias Jan 06 '20 at 18:35
  • I don't agree with this question being a duplicate of the two linked. https://stackoverflow.com/questions/48470049/build-a-json-string-with-bash-variables This one doesn't answer the main problem I had with generating a json string with an integer. https://stackoverflow.com/questions/48887711/how-to-convert-a-string-to-an-integer-in-a-json-file-using-jq This question asks about how to do it when using a specific tool. – Corey Jan 06 '20 at 19:55
  • The second fills in information missing from the first. Combine them, and you have a duplicate. Which is to say, I didn't mean to imply via the flagging that it was a duplicate of each preexisting question individually, but rather that we have multiple narrower preexisting questions that, combined, add up to the same thing. – Charles Duffy Jan 07 '20 at 03:54

1 Answers1

2

Do not ever generate JSON with string concatenation. The result is not guaranteed to be valid JSON (which any and every compliant parser will accept) unless it's generated by a compliant generator. For bash, one of the most widely-available tools for both purposes is jq:

# taken from the question
stringVar="L"; intVar=4

json=$(jq -n \
          --arg stringVar "$stringVar" \
          --arg intVar "$intVar" \
          '{"stringVar": $stringVar, "intVar": $intVar | tonumber}')
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Code above leads to `jq: error (at ): Expected JSON value (while parsing '')`. – accdias Jan 06 '20 at 18:41
  • Tested with jq-1.6 also. I did copy-and-paste as well. – accdias Jan 06 '20 at 18:43
  • @accdias, did you set the OP's `stringVar` and `intVar` first? – Charles Duffy Jan 06 '20 at 18:43
  • Nope! My bad. :-) – accdias Jan 06 '20 at 18:44
  • 1
    Heh. Yeah, trying to parse an empty string as an integer (as `$intVar | tonumber` does when `intVar` is empty, or otherwise not a number) will fail explicitly rather than generate incorrect JSON (as it would with the original code). That's a feature, not a bug. :) – Charles Duffy Jan 06 '20 at 18:44
  • For the variable being substituted in with arg it seems to work surrounded by quotes and without. Any reason for one vs. the other ? – Corey Jan 06 '20 at 19:23
  • 1
    @Corey, it only works without quotes while your test cases are simple. Change it to `stringVar='*'` or even just `stringVar='two words'` and then `--arg stringVar $stringVar` won't work anymore. (In general, bash is full of those kinds of subtle pitfalls, so trying to figure out what's "right" by trial-and-error is perilous; see [BashPitfalls](http://mywiki.wooledge.org/BashPitfalls) and the [BashFAQ](http://mywiki.wooledge.org/BashFAQ) for more examples of cases where robust answers are nonobvious). – Charles Duffy Jan 06 '20 at 20:16