Skip to content

Unambiguously quote and escape properties in JSON path rendering #1390

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 3 commits into from
Jul 17, 2025

Conversation

kurtmckee
Copy link
Contributor

@kurtmckee kurtmckee commented Jul 16, 2025

This PR introduces the following change:

  • Unambiguously quote and escape properties in JSON path rendering

Using the script from #1389, this is the new output with the changes in this branch:

$ python demo.py 
property=''             json_path="$['']"
property='['            json_path="$['[']"
property='.'            json_path="$['.']"
property='\\'           json_path="$['\\\\']"
property="'"            json_path="$['\\'']"
property=' '            json_path="$[' ']"
property='"'            json_path='$[\'"\']'
property='a[0]'         json_path="$['a[0]']"

Fixes #1389


📚 Documentation preview 📚: https://python-jsonschema--1390.org.readthedocs.build/en/1390/

@Julian
Copy link
Member

Julian commented Jul 16, 2025

Thanks for the PR (and issue).

I think we should consider and look at jsonpath-ng at very least to start with for testing. It's possible in the future we should deprecate the attribute and replace it with one that returns an object from that library, but even before we do that we can at least use it in our test cases to:

  • ensure all the tests you're adding actually are guarded to be valid JSON Paths
  • ensure we have covered at least the cases that that library itself tests

Could you have a look at doing that? As I say I'm good with just adding it as a test dependency for the moment and evaluating the deprecation/changing the actual interface down the line.

@Julian
Copy link
Member

Julian commented Jul 17, 2025

Looks great, nice work. Really appreciated!

@Julian Julian merged commit b5572c9 into python-jsonschema:main Jul 17, 2025
93 checks passed
@kurtmckee kurtmckee deleted the fix-json_path-str-rendering branch July 17, 2025 14:30
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.

_Error.json_path produces invalid or questionable JSON paths
2 participants