Skip to content

Fix union of quoted property names returning no results#268

Open
spokodev wants to merge 1 commit into
JSONPath-Plus:mainfrom
spokodev:fix/quoted-property-union
Open

Fix union of quoted property names returning no results#268
spokodev wants to merge 1 commit into
JSONPath-Plus:mainfrom
spokodev:fix/quoted-property-union

Conversation

@spokodev

Copy link
Copy Markdown
Contributor

Problem

A bracketed union of quoted property names — the canonical Goessner /
RFC-9535 form $['x','y'] — returns [] instead of the selected values:

JSONPath({ path: "$['x','y']", json: { x: 1, y: 2, z: 3 } });
// ACTUAL:   []
// EXPECTED: [1, 2]

Same for $["x","y"] and $['x', 'y']. The non-standard bare form $[x,y]
works ([1, 2]), and numeric unions $[0,2] work — only the quoted (standard)
form is broken.

Root cause

toPathArray only strips the surrounding quotes for a single-name bracket
(['name'], via /\[['"]([^'\]]*)['"]\]/). A multi-member bracket ['x','y']
doesn't match that pattern, so the interior quotes survive tokenization:

toPathArray("$['x','y']") → ["$", "x','y"]

The _trace union branch then does "x','y".split(',')["x'", "'y"] and
looks up properties literally named x' and 'y, which don't exist → [].

Fix

Strip a surrounding quote from each comma-separated member before the lookup.
Bare and numeric members carry no quotes, so they are unaffected (the strip is a
no-op for them).

Test

Added a "Union of quoted property names" case to test/test.properties.js
covering $['x','y'], $["x","y"], $['x', 'y']. Fails on main, passes with
the fix; the existing suite (280 tests) is unchanged.

Closes #159.

`$['x','y']` (the canonical bracketed union of quoted names) returned
`[]` instead of the selected values. `toPathArray` only strips the quotes
for a single-name bracket (`['name']`), so for a multi-member bracket the
interior quotes survive and the union branch splits `x','y` on the comma
into `["x'", "'y"]`, then looks up properties literally named `x'` and
`'y`, which don't exist.

Strip a surrounding quote from each comma-separated member before the
lookup. Bare (`$[x,y]`) and numeric (`$[0,2]`) members have no quotes and
are unchanged. Fixes the quoted forms `$['x','y']`, `$["x","y"]` and
`$['x', 'y']` (issue JSONPath-Plus#159).
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.

Quoted subscript can not work will with union/or subscripts.

1 participant