Nicolas S.
Nicolas S.

Reputation: 301

Write jq file with key value on sub object

I want to create a Json file (Composer) with JQ from nothing. My objective is to set module and version from args passed in jq command

{
    "require" : {
        "mymodule": "myversion"
   }
}

I tried something like this and I don't understand why it's not correct.

jq --arg module "themodule" --arg version "3.0" '{.require.($module):$version}' 

Thanks for your help

Upvotes: 1

Views: 151

Answers (1)

peak
peak

Reputation: 116977

First, you will almost certainly want to use the -n command-line option; second, module is a keyword and so cannot be used as a $-variable name. But:

jq -n --arg m themodule --arg version "3.0" '
  {require: { ($m): $version} }' 

produces:

{
  "require": {
    "themodule": "3.0"
  }
}

The trick here is to enclose the expression specifying the key-name in parentheses.

Upvotes: 2

Related Questions