Is there some way to use the name of an argument in the code?
For example, this command:
jq -n --arg name value '{($name): $name}'
has this output:
{
"value": "value"
}
Is it possible to get this output?:
{
"name": "value"
}
Is there some way to use the name of an argument in the code?
For example, this command:
jq -n --arg name value '{($name): $name}'
has this output:
{
"value": "value"
}
Is it possible to get this output?:
{
"name": "value"
}
If you remove the ($name): JQ will use the name as the key fallback:
$ jq -n --arg name value '{$name}'
{
"name": "value"
}
$
From the jq Frequently Asked Questions:
Notable Differences between Versions
: In which version was the abbreviation
{$x}for{"x": $x}introduced?Version 1.5
From the Object Construction ({}) documentation:
The value can be any expression (although you may need to wrap it in parentheses if, for example, it contains colons), which gets applied to the
{}expression's input (remember, all filters have an input and an output).{foo: .bar}will produce the JSON object
{"foo": 42}if given the JSON object{"bar":42, "baz":43}as its input. You can use this to select particular fields of an object: if the input is an object with "user", "title", "id", and "content" fields and you just want "user" and "title", you can write{user: .user, title: .title}Because that is so common, there's a shortcut syntax for it:
{user, title}
Not directly. You already know the name you want to use, so either hard code it:
jq -n --arg name value '{name: $name}'
or use a second variable
jq -n --arg key name --arg name value '{($key): $name}'
(I forgot about @0stone0's approach; that's probably what you want to use.)