RFC 9535: JSONPath: Queries for JSON documents
Summary
Stefan Gössner of Fachhochschule Dortmund, Glyn Normington and Carsten Bormann of the University of Bremen published the query language JSONPath in RFC 9535 in February 2024. Gössner had already proposed JSONPath in February 2007 as XPath for JSON, and numerous implementations adopted the idea in their own variants. The RFC creates a binding grammar for the first time and frees JSONPath from eval() and other quirks of individual programming languages.
From blog article to standard
JSONPath was intended as a lightweight companion to JSON in PHP and JavaScript. Instead of defining its own expression language, the original proposal handed parts of a query to the runtime, such as the JavaScript function eval(). With every new environment, queries became less portable as a result.
RFC 9535 defines the entire syntax itself, with segments for children and descendants, filter expressions and the functions length(), count(), match(), search() and value(). Regular expressions follow the lean I-Regexp format from RFC 9485. Backward compatibility with all previous variants was explicitly not a goal.
Security when evaluating
The security section warns clearly: implementations that pass query parts to eval() open the door to injection attacks. Naive recursive evaluation of the descendant segment can also lead to exponential runtime or stack overflow with specially crafted queries.
Anyone building queries from user input must validate and escape values as with SQL. And where JSONPath is part of a security mechanism, differences between implementations can themselves become an attack surface.
Ideas
- $ stands for the root node, @ for the current node in a filter.
- The descendant segment .. searches all levels of a document.
- Filter expressions select elements by conditions.
- Five built-in functions add length, count and pattern matching to filters.
- Normalized paths identify exactly one node unambiguously.
- An IANA registry takes up future function extensions.
Insights
- A popular format only becomes portable through its own grammar, not through references to the language it first ran in.
- Where query languages use foreign runtimes, they inherit their security problems.
Facts
- RFC 9535 appeared in February 2024 as a Proposed Standard and is 62 pages long.
- The underlying proposal by Stefan Gössner dates from February 2007.
- RFC 9535 registered the media type application/jsonpath.
- The functions match() and search() use regular expressions per RFC 9485.
References
- RFC 9535: JSONPath: Query Expressions for JSON
- Stefan Gössner: JSONPath, XPath for JSON (2007)
- RFC 9485: I-Regexp: An Interoperable Regular Expression Format (2023)
Remarks
- Two of the three editors work at German universities, in Dortmund and Bremen.
Recommendations
- When choosing a JSONPath library, look for explicit RFC 9535 conformance.
- Never build queries from user input by string concatenation; escape values instead.
- Limit runtime and recursion depth for untrusted queries.
Read the RFC at the RFC Editor
Found a mistake or have something to add? Get in touch.
Links to the original source and the Web Archive open in a new tab.