JQ Create JSON Key and Value Pair
JQ Create JSON Key and Value Pair
I'm trying to create a JSON file with this very simple command:
jq -n --arg greeting world --arg mykey hello '"hello":$greeting'
jq -n --arg greeting world --arg mykey hello '"hello":$greeting'
My problem is that when I replace the key with $mykey I get this error:
# jq -n --arg greeting world --arg mykey hello $mykey:$greeting
jq: error: syntax error, unexpected ':' (Unix shell quoting issues?) at <top-level>, line 1:
:
jq: 1 compile error
How can I create a simple JSON file with two arguments/variables?
1 Answer
1
As explained in the jq manual, when a key name is specified programmatically, the defining expression must be enclosed in parentheses:
$ jq -n --arg greeting world --arg mykey hello '($mykey):$greeting'
"hello": "world"
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of Parentheses in JQ for .key
– Jeff Mercado
Sep 8 '18 at 3:22