Skip to content

fix(jq): support quoted string key shorthand in object construction#108

Open
skoblenick wants to merge 3 commits intovercel-labs:mainfrom
skoblenick:skoblenick/fix-jq-shorthand-object-construction
Open

fix(jq): support quoted string key shorthand in object construction#108
skoblenick wants to merge 3 commits intovercel-labs:mainfrom
skoblenick:skoblenick/fix-jq-shorthand-object-construction

Conversation

@skoblenick
Copy link
Copy Markdown
Contributor

Problem

The jq parser did not support quoted string key shorthand syntax in object construction. In real jq, {"name"} is valid shorthand for {"name": .name}, allowing users to quickly project fields from an object. Our parser required a colon and explicit value after every quoted string key, causing expressions like {"name"}, {"name", "label"}, and keyword keys like {"if"} to fail with a parse error.

This broke real-world pipelines such as:

echo '{"id":"abcde","name":"foo","label":"bar"}' | jq -c '{"name", "label"}'
# Expected: {"name":"foo","label":"bar"}
# Got: parse error

Fix

Updated the query-engine parser (src/commands/query-engine/parser.ts) to handle quoted string keys the same way as unquoted identifier keys: when a quoted string key is not followed by a colon, it is treated as shorthand — the key becomes the string value and the value becomes .key (a field access). When a colon follows, the existing explicit key-value behavior is preserved.

Tests

  • Unit tests (src/commands/jq/jq.string-key-shorthand.test.ts): 21 tests covering parser AST correctness and evaluation for standard keys, keyword keys ("if", "true", "null", "as", "try"), non-identifier keys ("a-b"), numeric string keys ("1"), empty string keys (""), mixed shorthand + explicit entries, and fromjson pipelines.
  • Comparison tests (src/comparison-tests/jq.comparison.test.ts): 8 tests validating output parity with real bash jq across all shorthand variants.

@vercel
Copy link
Copy Markdown

vercel bot commented Feb 10, 2026

@skoblenick is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

@skoblenick skoblenick force-pushed the skoblenick/fix-jq-shorthand-object-construction branch from a6432d5 to 01a8e76 Compare February 21, 2026 05:58
…ction

In real jq, quoted string keys can be used as shorthand in object
construction without a colon, e.g. {"name"} is equivalent to
{"name": .name}. The parser does not support this — the STRING branch
in parseObjectConstruction() (parser.ts:1032-1035) unconditionally
requires a colon after string keys, causing a parse error.

This affects patterns like:
  {"name"}           -> Expected ":" error
  {"name", "label"}  -> Expected ":" error
  {"if"}             -> Expected ":" error (keyword as string key)
  {"name", "v": .x}  -> Expected ":" error (mixed shorthand/explicit)

Note: unquoted identifier shorthand ({name, label}) works correctly
since isIdentLike() handles IDENT and keyword tokens. The bug is
specifically in the quoted string key path.

Adds 15 unit tests and 5 comparison tests (all currently failing).
The parser unconditionally required a colon after string keys in object
literals, causing filters like {"name"} and {"if"} to fail with a parse
error. Real jq treats these as shorthand for {"name": .name}.

Mirror the existing isIdentLike() shorthand logic in the STRING
branch by checking for a colon and falling back to a Field
access node when absent.
…nd edge cases

- Assert entry count, key strings, and Field node shape in parser tests
- Add non-identifier key tests: hyphenated ("a-b"), numeric ("1"), empty ("")
- Add regression test for explicit key-value with string key ("name": .x)
- Add comparison tests for non-identifier key shorthands
- Extract expectShorthandEntry() helper to reduce duplication
@skoblenick skoblenick force-pushed the skoblenick/fix-jq-shorthand-object-construction branch from 01a8e76 to edcebfa Compare March 17, 2026 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant