Skip to content

Can't chain generic object indexes #2512

Closed
@partap

Description

@partap

Describe the bug
When selecting subfields (e.g. .foo.bar), using the generic notation (e.g. .["foo"].["bar"]) throws a syntax error.

To Reproduce
Given the input (sample.json):

{
  "title": "Title 1",
  "media:content": {
    "media:credits": "media:content.media:credits 1"
  }
}

I want to pull .media-content.media:credits to the top level and relabel the field to "credits" like this:

$ cat sample.json | jq '{title, credits: .["media:content"].["media:credits"]}'

Expected output

{
  "title": "Title 1",
  "credits": "media:content.media:credits 1"
}

Actual output

jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
{title, credits: .["media:content"].["media:credits"]}
jq: 1 compile error

Environment (please complete the following information):

  • OS and Version: macOS Ventura 13.1
  • jq version: 1.6

Additional context
If the keys can be used in identifier-like syntax, like this, then it works:
(sample2.json)

{
  "title": "Title 1",
  "content": {
    "credits": "content.credits 1",
  },
}
$ cat sample2.json | jq '{title, credits: .content.credits}'
{
  "title": "Title 1",
  "credits": "content.credits 1"
}

But even then , it still doesn't work with the generic quoted syntax chained after the first selector

$ cat sample2.json | jq '{title, credits: .["content"].["credits"]}'  # Error
$ cat sample2.json | jq '{title, credits: .content.["credits"]}'      # Error
$ cat sample2.json | jq '{title, credits: .["content"].credits}'      # Works

Addendum:
While writing this, I did finally discover a workaround using the pipe operator:

$ cat sample2.json | jq '{title, credits: .["content"] | .["credits"]}'.            # Works
$ cat sample.json | jq '{title, credits: .["media:content"] | .["media:credits"]}'. # Works

But I'm filing the bug report anyways because, according to the docs, .foo.bar should be equivalent to .["foo"].["bar"].
(and likewise, .["foo"].["bar"] should be equivalent to .["foo'] | .["bar"]

Thanks for the tool!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions