简体   繁体   中英

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}'

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?

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"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM