JSONPathBuilder
JSONPathBuilder provides a type-safe way to traverse JSON structures and build JSON path expressions. It’s created by calling eb.ref(column, '->$') or eb.jsonPath<'column'>() on an ExpressionBuilder.
Type Parameters
any
required
The source type (root JSON type)
any
default:"S"
The current output type at this point in the traversal
Methods
at
Access an element of a JSON array in a specific location.number | 'last' | '#-{number}'
required
Array index,
last for MySQL, or #-N for SQLite (e.g., #-1 for last element)$assertType to narrow the type safely.
See also key to access properties of JSON objects.
Example:
key:
key
Access a property of a JSON object.string
required
Object property key
at to access elements of JSON arrays.
Example:
at:
TraversedJSONPathBuilder
Returned byat() and key() calls. Extends JSONPathBuilder with additional methods for aliasing and type casting.
as
Returns an aliased version of the expression.string | Expression
required
The alias name for the expression
as "the_alias" to the end of the SQL, this method also provides strict typing.
Example:
$castTo
Change the output type of the json path.type parameter
required
The new output type
JSONPathBuilder with a new output type.
Example:
$notNull
Omit null from the expression’s type.this with a new output type.
toOperationNode
Converts the builder to an operation node for internal use.AliasedJSONPathBuilder
Returned byas() calls. Represents a JSON path expression with an alias.
any
required
The output type
string
required
The alias name
Usage Patterns
JSON Reference (PostgreSQL/SQLite)
Use-> operator for JSON references that chain operators:
JSON Path (MySQL)
Use->$ operator for JSON path expressions:
JSON Path Expression
UsejsonPath() for creating path expressions to use in functions:
Type Safety
TheJSONPathBuilder maintains full type safety when traversing JSON structures:
- Object property types from the schema
- Array element types
- Nullability based on optional fields and array access
- Proper return types at each traversal step