Skip to content

[JsonPath] Always use brackets notation with JsonPath::key() #60668

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

Merged
merged 1 commit into from
Jun 4, 2025

Conversation

alexandre-daubois
Copy link
Member

Q A
Branch? 7.3
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #60664
License MIT

As explained in the issue, the dot notation can lead to invalid paths when used with some identifiers (for example, when it contains a -). The issue gives the following example:

use Symfony\Component\JsonPath\JsonCrawler;
use Symfony\Component\JsonPath\JsonPath;

$jsonPathExpression = (string) (new JsonPath)->key('some-prop');

dump((new JsonCrawler('{"some-prop": "example value"}'))->find($jsonPathExpression));

Let's always use the brackets notation in JsonPath::key(). Dot notation is only a shorthand to the verbose brackets version and this solves the problem without having to do further analysis on the key.

Added a few tests to ensure the brackets notation works well too.

Copy link
Member

@stof stof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest also adding a test covering the usage of $["*"] to access the * key (instead of all keys), as you increase test coverage.

@alexandre-daubois alexandre-daubois force-pushed the jsonpath-brackets branch 2 times, most recently from ebf545c to 55dda22 Compare June 4, 2025 06:50
@nicolas-grekas
Copy link
Member

Thank you @alexandre-daubois.

@nicolas-grekas nicolas-grekas merged commit e9eb3e7 into symfony:7.3 Jun 4, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The JsonPath path builder produces an invalid JsonPath expressions for keys that are not valid identifiers
4 participants