JSON5
Query Language
Path expressions over a JSON5Value
The json5/query subpackage provides a tiny path expression language for pulling values out of a parsed JSON5Value tree. A query is parsed by QueryParser into a QueryExpression, then evaluated against a value by QueryEngine.
Syntax
A query is a chain of accessors starting from the root:
.- the root value (the parsed tree itself)..field- field access on aJSON5Object.[index]- element access on aJSON5Array(integer index) orJSON5Object(string or number index).
Combine accessors to drill in:
.user.address[0].zip
reads value.user.address[0].zip.
Expression types
Each accessor compiles to one of these QueryExpression nodes:
RootExpression- the leading.. Returns the value the query is run against.FieldAccessExpression- the.nameform. Returns the named field of an object, or null if absent.IndexExpression- the[expr]form. The inner expression is one of:NumberExpression- integer literal, used for array indexing.StringExpression- quoted string, used for object key access.
Running a query
query = QueryParser.parseExpression(".user.name")
result = QueryEngine.eval(query, parsedValue)
result is itself a JSON5Value. To pull out a JVM-native string or number, downcast to the concrete type (JSON5String, JSON5Integer, etc.) and read its payload.
See also
- Value Types for what queries return.
- Parsing JSON5 for producing the tree to query.