Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure to parse .x.["y.z"] notation #1168

Closed
mcint opened this issue Jun 21, 2016 · 14 comments
Closed

Failure to parse .x.["y.z"] notation #1168

mcint opened this issue Jun 21, 2016 · 14 comments

Comments

@mcint
Copy link

mcint commented Jun 21, 2016

mcint@work MINGW64 /c/git

The document

$ less test.json | jq -c '.'
{"a":"A","b":{"c":{"d.e.f":"DEF"}}}

The complex key

$ less test.json | jq -c '.b.c'
{"d.e.f":"DEF"}

One naive approach

$ less test.json | jq -c '.b.c.d.e.f'
null

Solution proposed by the docs

$ less test.json | jq -c '.b.c.["d.e.f"]'
jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Windows cmd shell quoting issues?) at <top-level>, line 1:
.b.c.["d.e.f"]
jq: 1 compile error

Work-around

$ less test.json | jq -c '.b.c | .["d.e.f"]'
"DEF"

Boundaries of quoted key solution

$ less test.json | jq -c '.b | .c.["d.e.f"]'
jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Windows cmd shell quoting issues?) at <top-level>, line 1:
.b | .c.["d.e.f"]
jq: 1 compile error
@mcint
Copy link
Author

mcint commented Jun 21, 2016

Trials with 2-part ("z.y") versus 3-part ("z.y.x") key

$ less test2.json | jq -c '.b.c'
{"d.e": "DEF"}
$ less test2.json | jq -'c .b.c.d.e'
null
$ less test2.json | jq -c '.b.c.["d.e"]'
jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Windows cmd shell quoting issues?) at <top-level>, line 1:
.b.c.["d.e"]
jq: 1 compile error

@ghost
Copy link

ghost commented Jun 21, 2016

The syntax is either .foo (for keys without extraneous characters) or ["foo.bar"] (note the absence of a trailing dot). That is:

jq '.b | .c["d.e.f"]'
jq '.b.c["d.e"]'

So, you may be wondering: how come jq '.b.c | .["d.e.f"]' does work? The answer lies in that the output of the previous filter is represented by .. Therefore, .["d.e.f"] performs ["d.e.f"] over .. The dot can be elided when using .foo as the first filter: that's why it's .b.c and not ..b.c.

@mcint
Copy link
Author

mcint commented Jun 21, 2016

Great, that completely addresses my question, thank you. Good to close.

For the sake of completeness, it looks like the dot must be elided.

$ less test2.json | jq '..b.c'
jq: error: syntax error, unexpected IDENT, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1:
..b.c
jq: 1 compile error

@ghost
Copy link

ghost commented Jun 21, 2016

Yes, that is correct: the dot must be elided. I misspoke in my earlier comment.

@rogpeppe
Copy link

This seems to be a documentation issue. The reference documentation
describes the operator as .[0] and says:

When the index value is an integer, .[<value>] can index arrays

This appears to be true only if the dot is at the start. There
are no examples of an array index not at the start, and no
indications that I've found that the dot is not allowed in that
case.

@wtlangford
Copy link
Contributor

wtlangford commented May 11, 2017 via email

@nicowilliams
Copy link
Contributor

My question is: should we add .a.["b"]? I'm tempted to say "no", but this does come up frequently enough that maybe it'd just be for the best. What do we lose if we add this?

@MondayHopscotch
Copy link

This is something I recently ran into, as well. Simplified examples using JQPlay:

With dot (syntax error):
https://jqplay.org/s/itgHiQlNMP

Without dot (happy):
https://jqplay.org/s/AWemvC2Vsv

Is this something that there is a concrete reason behind disallowing? Or is it just a limitation of the current implementation?

Wording my question another way: Is there any reason that supporting this in the future would be explicitly unwanted?

@nicowilliams
Copy link
Contributor

@MondayHopscotch There is no limitation of the current implementation that makes this difficult to support.

As to reasons not to support this, I don't think there are any, and this is a very very FAQ.

This trivial patch adds support for this:

diff --git a/src/parser.y b/src/parser.y
index 6255850..39c7836 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -685,6 +685,12 @@ Term '[' Exp ']' '?' {
 Term '[' Exp ']' %prec NONOPT {
   $$ = gen_index($1, $3);
 } |
+Term '.' '[' Exp ']' '?' {
+  $$ = gen_index_opt($1, $4);
+} |
+Term '.' '[' Exp ']' %prec NONOPT {
+  $$ = gen_index($1, $4);
+} |
 Term '[' ']' '?' {
   $$ = block_join($1, gen_op_simple(EACH_OPT));
 } |
$ ./jq -cn '{a:{b:{c:"d"}}}|.a.["b"]'
{"c":"d"}
$ 

@nicowilliams
Copy link
Contributor

@wtlangford any objections to pushing that patch?

@rogpeppe
Copy link

rogpeppe commented Mar 8, 2019

The syntax is either .foo (for keys without extraneous characters) or ["foo.bar"] (note the absence of a trailing dot).

Perhaps the real issue here is that the documentation doesn't explain the grammar clearly. I can't see anything in the documentation that mentions that [] can be used as an operator at all.

Note that .a.b.c is the same as .a | .b | .c

AFAICS . is being used in two senses here - as an initial value (the first .) and then as a special case as a binary operator. Currently the filter documentation elides the operands for each operator. Perhaps if it didn't, then things might be more obvious.

One other thing: .x.y? works, and the docs document the .foo? form exactly the same was as .[<string>] so it's perhaps not surprising people get confused when .x.["<string>"] doesn't work.

@nicowilliams
Copy link
Contributor

There is certainly a bug in the manual...

   For  example  .["foo::bar"]  and .["foo.bar"] work while .foo::bar does
   not, and .foo.bar means .["foo"].["bar"].

@nicowilliams
Copy link
Contributor

@rogpeppe https://github.com/stedolan/jq/wiki/jq-Language-Description

@nicowilliams nicowilliams self-assigned this Mar 25, 2019
@nicowilliams
Copy link
Contributor

I've pushed 3e0a118 to allow '.foo.["bar"] syntax.

fuzzypixelz added a commit to fuzzypixelz/zenoh-python that referenced this issue Apr 17, 2024
ubuntu-22.04 runners use jq 1.6 which doesn't recognize a dot for `[]` value iterator.

See: jqlang/jq#1168.
fuzzypixelz added a commit to fuzzypixelz/zenoh-kotlin that referenced this issue Apr 17, 2024
ubuntu-22.04 runners use jq 1.6 which doesn't recognize a dot for `[]` value iterator.

See: jqlang/jq#1168.
fuzzypixelz added a commit to fuzzypixelz/zenoh-c that referenced this issue Apr 17, 2024
ubuntu-22.04 runners use jq 1.6 which doesn't recognize a dot for `[]` value iterator.

See: jqlang/jq#1168.
Mallets pushed a commit to eclipse-zenoh/zenoh-python that referenced this issue Apr 17, 2024
* build: Get Read the Docs release number from Cargo manifest

This avoids hardcoding the release number in the documentation build config,
making it easier to bump the version by only modifying the manifest.

* build: Require Python 3.11 in Read the Docs configuration

Python 3.11 is needed to access tomllib; useful for parsing the Cargo manifest file.

* feat: Create branch, bump version and tag in release workflow

* feat: Add publish-github job

* fix: Broken tag dependencies

* chore: Remove enforce-linking-issues workflow

* fix: Bump version in pyproject.toml

* chore: Upgrade artifact actions from v3 to v4

* fix: Typo in git-commit command

* fix: Support jq 1.6

ubuntu-22.04 runners use jq 1.6 which doesn't recognize a dot for `[]` value iterator.

See: jqlang/jq#1168.

* Revert "chore: Upgrade artifact actions from v3 to v4"

This reverts commit a535971.

* fix: Build wheels from release branch

* fix: Switch to pypa/gh-action-pypi-publish@release/v1

The older actions doesn't recognize the pyproject.toml metadata fields.
Mallets pushed a commit to eclipse-zenoh/zenoh-kotlin that referenced this issue Apr 17, 2024
* feat: Store project version in version.txt

* fix: Remove cargo-deb metadata section in `zenoh-jni`

* feat: Add `bump-and-tag.bash` script

* chore: Rename release workflow to pre-release

* chore: Disable release event on publish workflows

* fix: Typo in ci/scripts

* feat: Automate Release

* chore: Remove enforce-linking-issues workflow

* fix: Set permissions for publishing jobs

* feat: Add publish-dokka to release workflow

* fix: Set release branch in publishing jobs

* fix: Make `branch` input unrequired in publishing jobs

* style: Uniformize publishing workflow names

* fix: Unecessary use of `BOT_TOKEN_WORKFLOW`

* fix: Incorrect Cargo manifest/lockfile path

* fix: Add missing `live-run` input for publish-dokka job

* fix: Typo in pre-release workflow name

* fix: Remove `if: always()` in publish-github job

* fix: Remove unecessary SSH passphrase/privatekey inputs

* chore: Retrigger CI

* fix: Support jq 1.6

ubuntu-22.04 runners use jq 1.6 which doesn't recognize a dot for `[]` value iterator.

See: jqlang/jq#1168.
wyfo pushed a commit to ZettaScaleLabs/zenoh-python that referenced this issue Apr 25, 2024
* build: Get Read the Docs release number from Cargo manifest

This avoids hardcoding the release number in the documentation build config,
making it easier to bump the version by only modifying the manifest.

* build: Require Python 3.11 in Read the Docs configuration

Python 3.11 is needed to access tomllib; useful for parsing the Cargo manifest file.

* feat: Create branch, bump version and tag in release workflow

* feat: Add publish-github job

* fix: Broken tag dependencies

* chore: Remove enforce-linking-issues workflow

* fix: Bump version in pyproject.toml

* chore: Upgrade artifact actions from v3 to v4

* fix: Typo in git-commit command

* fix: Support jq 1.6

ubuntu-22.04 runners use jq 1.6 which doesn't recognize a dot for `[]` value iterator.

See: jqlang/jq#1168.

* Revert "chore: Upgrade artifact actions from v3 to v4"

This reverts commit a535971.

* fix: Build wheels from release branch

* fix: Switch to pypa/gh-action-pypi-publish@release/v1

The older actions doesn't recognize the pyproject.toml metadata fields.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants