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
The source type (root JSON type)
The current output type at this point in the traversal
Methods
at
Access an element of a JSON array in a specific location.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.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.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.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.
The output type
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