01mf02 / jaq

A jq clone focussed on correctness, speed, and simplicity
MIT License
2.7k stars 67 forks source link

Object identifier-index with space #141

Closed sanpii closed 10 months ago

sanpii commented 10 months ago

Keys are string and can contain spaces.

$ echo '{"key with space": 10}' | jaq '.key with space'
Error: Unexpected token, expected +, and, end of input, ==, ?, <=, +=, as, *, |=, -=, /, or, !=, |, >=, ,, /=, *=, [, %, <, //, >, -, =, %=, .
   ╭─[<unknown>:1:6]
   │
 1 │ .key with space
   │      ──┬──
   │        ╰──── Unexpected token with
───╯

Expected:

$ echo '{"key with space": 10}' | jaq '.key with space'
10
pkoppstein commented 10 months ago

It is a well-known fact that, in the jq family of languages (jq, gojq, jaq, ...), the abbreviated form .key cannot be used for keys with non-alphanumeric characters other than the underscore. One possibility is simply to use double-quotation marks:

$ echo '{"key with space": 10}' | jaq '."key with space"'

Here, the single-quotes are only needed to pass the text of the jaq program to the jaq interpreter.

wader commented 10 months ago

@pkoppstein did you mean jaq '."key with space"'?

@sanpii if this looks confusing maybe it clarifies things to think about it as .abc and ."abc" being shorthands for .["abc"]

sanpii commented 10 months ago

Thank you for these clarifications.