Skip to content

No (documented) shorthand way to copy nested objects in Object Construction #2578

@garethhumphriesgkc

Description

@garethhumphriesgkc

Describe the bug
We can easily copy fields from the root of the input object into our output by using { field } syntax, but there is no way to copy nested fields with the same shorthand. I can't say { fields.field }, I must use the cumbersome { fields: { field: .fields.field } }.

This is especially tedious when your desired field is many layers deep and you want to keep it in context.

While I appreciate there can be questions around what the expected output is when navigating nested objects, I think in this case the same short hand logic makes unambiguous sense - it is still "create a field called this, with the contents of this", it's just that now this has multiple layers to it.

To Reproduce

$ echo '{"a":{"b":1}}' | jq '{ a }'
{
  "a": {
    "b": 1
  }
}
$ echo '{"a":{"b":1}}' | jq '{ a.b }'
jq: error: syntax error, unexpected FIELD, expecting '}' (Unix shell quoting issues?) at <top-level>, line 1:
{a.b}  
jq: 1 compile error
$

Expected behavior
Output should include the value of the selected field

$ echo '{"a":{"b":1,"junk":0},"useless":0}' | jq '{ a.b }'
{
  "a": {
    "b": 1
  }
}
$

Environment (please complete the following information):
jq-1.6

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions