3

I have a small bash script that uses jq to construct a JSON object from user input. Unfortunately, I am unable to convince jq to accept a multi word variable as a value to one of its keys.

Here is an equivalent example from the bash prompt:

With no blank spaces jq works as I expected:

$> value="Input"
$> jq -n --arg value $value '{"key": ($value)}'

returns:

{
   "key": "Input"
}

But it breaks with a multi-word value:

$> value="A multi word input"
$> jq -n --arg value $value '{"key": ($value)}'

returns an error:

jq: error: multi/0 is not defined at <top-level>, line 1:
multi
jq: 1 compile error

What's the magic that will convince jq not to choke on the white space of $value?

stefano
  • 133

1 Answers1

3

Your unquoted $value is subject to word splitting by the shell - the answer is simply to quote it:

$ jq -n --arg value "$value" '{"key": ($value)}'
{
  "key": "A multi word input"
}

See also

steeldriver
  • 81,074