mirror of
https://github.com/jqlang/jq.git
synced 2025-04-18 17:24:01 +03:00
Allow variable refs as object keys {$key:value}
Users are often surprised by the requirement to parenthesize any non-trivial object key expressions in object constructors. E.g., {"a"+"b":1}. This commit adds one more kind of key expression besides literals and idents: variable references. A common use case for this is jq programs as JSON templates to fill in with variables computed from inputs or passed in on the command-line. E.g., {some_key:$value}. Now users can also use, e.g., {$key:$value}. This and the restrictions on key and value expressions in object constructors are now clarified a bit in the documentation.
This commit is contained in:
parent
bb87e6e30f
commit
8ea4a5584e
@ -549,13 +549,16 @@ sections:
|
|||||||
dictionaries or hashes), as in: `{"a": 42, "b": 17}`.
|
dictionaries or hashes), as in: `{"a": 42, "b": 17}`.
|
||||||
|
|
||||||
If the keys are "identifier-like", then the quotes can be left
|
If the keys are "identifier-like", then the quotes can be left
|
||||||
off, as in `{a:42, b:17}`. Keys generated by expressions need
|
off, as in `{a:42, b:17}`. Variable references as key
|
||||||
to be parenthesized, e.g., `{("a"+"b"):59}`.
|
expressions use the value of the variable as the key. Key
|
||||||
|
expressions other than constant literals, identifiers, or
|
||||||
|
variable references, need to be parenthesized, e.g.,
|
||||||
|
`{("a"+"b"):59}`.
|
||||||
|
|
||||||
The value can be any expression (although you may need to
|
The value can be any expression (although you may need to wrap
|
||||||
wrap it in parentheses if it's a complicated one), which gets
|
it in parentheses if, for example, it contains colons), which
|
||||||
applied to the {} expression's input (remember, all filters
|
gets applied to the {} expression's input (remember, all
|
||||||
have an input and an output).
|
filters have an input and an output).
|
||||||
|
|
||||||
{foo: .bar}
|
{foo: .bar}
|
||||||
|
|
||||||
@ -593,6 +596,16 @@ sections:
|
|||||||
|
|
||||||
{"stedolan": ["JQ Primer", "More JQ"]}
|
{"stedolan": ["JQ Primer", "More JQ"]}
|
||||||
|
|
||||||
|
Variable references as keys use the value of the variable as
|
||||||
|
the key. Without a value then the variable's name becomes the
|
||||||
|
key and its value becomes the value,
|
||||||
|
|
||||||
|
"f o o" as $foo | "b a r" as $bar | {$foo, $bar:$foo}
|
||||||
|
|
||||||
|
produces
|
||||||
|
|
||||||
|
{"f o o":"f o o","b a r":"f o o"}
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
- program: '{user, title: .titles[]}'
|
- program: '{user, title: .titles[]}'
|
||||||
input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
|
input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
|
||||||
|
1161
src/parser.c
1161
src/parser.c
File diff suppressed because it is too large
Load Diff
@ -928,6 +928,10 @@ IDENT ':' ExpD {
|
|||||||
$$ = gen_dictpair($1, BLOCK(gen_op_simple(POP), gen_op_simple(DUP2),
|
$$ = gen_dictpair($1, BLOCK(gen_op_simple(POP), gen_op_simple(DUP2),
|
||||||
gen_op_simple(DUP2), gen_op_simple(INDEX)));
|
gen_op_simple(DUP2), gen_op_simple(INDEX)));
|
||||||
}
|
}
|
||||||
|
| '$' IDENT ':' ExpD {
|
||||||
|
$$ = gen_dictpair(gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($2))),
|
||||||
|
$4);
|
||||||
|
}
|
||||||
| '$' IDENT {
|
| '$' IDENT {
|
||||||
$$ = gen_dictpair(gen_const($2),
|
$$ = gen_dictpair(gen_const($2),
|
||||||
gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($2))));
|
gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($2))));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user