Build JSON with a variable key using jq

I am trying to use jqto build a hash in which the key name comes from a variable. Something like that:

jq --null-input --arg key foobar '{$key: "value"}'

However, this does not work and gives the following error:

error: syntax error, unexpected '$'
{$key: "value"} 1 compile error
+4
source share
1 answer

Use parentheses to evaluate $keyearlier than in:

 jq --null-input --arg key foobar '{($key): "value"}'

See also: Brackets in JQ for .key

+8
source

All Articles