Jq: `.foo.[]` is a syntax error (but not `.foo | .[]` or `.foo[]`)

Created on 31 Jul 2018  ·  7Comments  ·  Source: stedolan/jq

Hello,

First, thank you for your work on jq!

As I couldn't find any issue matching “.[]”, I thought I'd open this: jq '.foo | .[]' works as expected, but jq '.foo.[]' barfs out a syntax error (actually, jq '.foo[]' works too). It's not a big deal given there's the alternative syntax, but it's not really intuitive (as .a.b and .a | .b are equivalent as per the docs), and at first I thought I had got the .[] wrong. :)

Most helpful comment

.foo.[0] would avoid syntax errors, and I don't think it would do any harm, since [ is a special character, and could not be confused with a field name. And I don't think there's any other case where a . could follow .foo.

But it is ugly.

All 7 comments

As you note, the correct abbreviation of .foo|.[] is .foo[] and indeed E|.[F] can usually be abbreviated to E[F] or at least (E)[F], where F may be omitted. Adding support for a half-measure abbreviation would, in my opinion, just complicate things and, worse, decrease the readability of jq, by increasing the clutter.

Well, I just personally thought that inconsistent, but maybe that's just a documentation issue. Anyways, the current documentation that states that “Note that .a.b.c is the same as .a | .b | .c.” should get a warning that it doesn't work with .[] if this is not going to be changed, I think :)

Also, I just re-read diagonally across the first three sections of the manual, and couldn't find an easy-to-see example of the .foo[] syntax, so that's at least a doc issue :)

Yes, the documentation in the manual should be improved as you suggest.

Meanwhile, see the jq FAQ:

𝑸: How can I access the value of a key with hyphens or $ or other special characters in it? Why does .a.["$"] produce a syntax error?

There is an element of inconsistency, but it lies in always having a leading . and yet this isn't repeated for a name lookup e.g. it's not ..a.

The syntax .foo[i] itself is quite natural, the same as javascript and other languages with c-like syntax, for a variable foo in scope, that is an array.

IMO for consistency, either .foo.[0] or .foo | [0] should be allowed :) (well, for pure consistency, both, but I guess it's possible to just allow one of the two and deprecate the equivalent .foo | .[0] or .foo[0] depending on the choice)

Then, so long as the docs are clear, I personally don't care for a little bit of inconsistency for a tool I rarely use with longer-than-50-char commands :)

Note that [0] is valid JSON and therefore the second alternative is impossible.

Deprecating .foo | .[0] in jq would be like deprecating 1 + 1 in arithmetic, i.e. effectively meaningless.

.foo.[0] would avoid syntax errors, and I don't think it would do any harm, since [ is a special character, and could not be confused with a field name. And I don't think there's any other case where a . could follow .foo.

But it is ugly.

Was this page helpful?
0 / 5 - 0 ratings