-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Construct literal objects with subsequences of key-value pairs #2529
Comments
$ jq -n '["foo", "bar", "baz", "quux"] | map({(.): .}) | add'
{
"foo": "foo",
"bar": "bar",
"baz": "baz",
"quux": "quux"
} |
Ok, yea. In reality I'm using jq as a templating language; my expression has null input, is somewhat large, and the object in question is nested a few layers deep. In this sense the outer But this works:
One could imagine parenthesizing the inner-most object in my tree, and having some multi-line construction like this:
|
So you can close this issue or you want to discuss on the new syntax? I think the proposed syntax has various difficulties, especially on rejecting invalid queries like |
You can define your object as an array of key value pairs: [
["key1", "value"],
["key2", {foo: "bar"}]
]
# instead of
{
key1: "value"
key2: {foo: "bar"}
} And then call [
["foo", "foo"],
["bar", "bar"],
("baz", "quux" | [., .])
] |
map({ key: .[0], value: .[1] }) |
from_entries Calling [
["foo", "foo"],
["bar", "bar"],
("baz", "quux" | [., .])
| { key: .[0], value: .[1] }
] |
from_entries |
My immediate need has been addressed, we can close. sits back in armchair As a hypothetical, the idea is still fun to kick around.
Right. As a first step, we could ask "What is the Type checks would then prevent values of the new kind from appearing in most contexts. Your |
When i was messing with jqjq i was thinking a bit why only array syntax can "collect" but not object, would be useful if it could i think. Haven't thought deeply about possible syntax for it, would it help if the syntax was something like:
a key-less expression (i guess parentheses would be required?) inside an object literal can output objects that will be merged? |
@wader What happens when you extract the query to declaration? jq -n '{f:1,g:2,h:3}; {f}' # { f: 1 }
jq -n '{f:1,g:2,h:3} | def f: "x"; {(f):1}' # { x: 1 }
jq -n '{f:1,g:2,h:3} | def f: {x:0}; {(f)}' # looks close and currently error, but should be {x:0}??? |
Aha sorry that example was not what i intended, in the case @phs want, key and value from dot it would have to be like this to use current object syntax:
Now i see, yes that is not good. Would requiring parentheses help? ex:
Would not be a key-less "pair"? so no change
Yeap would be To add an example Sorry for just shooting syntax proposals without much thought :) |
Describe the bug
I want a syntax to construct objects with subsequences of key-value pairs.
To Reproduce
Consider this construction with an array:
We use a parenthesized expression within a literal array that expands to a sequence of values, which are then inlined into the array.
I would like to do the same with key-value pairs within literal objects. Consider this example:
Expected behavior
Instead of erroring out, I would like to see this output:
Environment (please complete the following information):
jq 1.6 on Pop!_OS 22.04
The text was updated successfully, but these errors were encountered: