JSON and JSONB types
Exposed works together with the JSON serialization library of your choice by allowing column definitions that accept generic serializer and deserializer arguments through the json() and jsonb() functions.
Databases store JSON values in either text or binary format, so Exposed provides a separate type for each.
Add dependencies
Before using JSON and JSONB column types or functions, add the exposed-json module to your build file:
Basic usage
The following example uses kotlinx.serialization with a @Serializable class. This overload of json() accepts a Json configuration and uses the KSerializer for the specified type:
You can also provide serializer and deserializer functions directly. For example, the following definition uses Jackson with the jackson-module-kotlin dependency and the full form of json():
Insert and update JSON data
The following examples use the TeamsTable definition from the kotlinx.serialization example.
To store a JSON value, assign an instance of the serializable class to the column. Exposed serializes the value using the Json instance passed to the jsonConfig parameter of json():
To modify a stored value, assign a new instance in an update() statement:
When you read the column, Exposed deserializes the stored JSON back into an instance of the class:
Store arrays
JSON columns can also store arrays. Pass the corresponding Kotlin array type to json(), for example IntArray for integers or Array<Project> for objects:
To insert values into these columns, use standard Kotlin collections:
Supported types
The exposed-json module provides the following column types:
Column type | PostgreSQL | MySQL / MariaDB / H2 | SQLite | SQLServer | Oracle |
|---|---|---|---|---|---|
|
|
|
|
| |
|
|
| Not supported | Not supported |
The exact SQL type depends on the database dialect. For example, jsonb() maps to JSON in MySQL and H2 rather than to a type named JSONB.
json()
Use json() to define a column that stores JSON data in a text-based representation.
When using kotlinx.serialization, pass the Json instance to the jsonConfig parameter:
jsonb()
Use the jsonb() to define a column for JSON data that the database can store in a binary representation, where supported.
When using kotlinx.serialization, pass the Json instance to the jsonConfig parameter:
JSONB support in SQLite
SQLite supports storing JSON data in its binary JSONB format starting with version 3.45.0.0. Exposed maps jsonb() columns to BLOB and wraps values written to them with SQLite's JSONB() function.
This applies to values in DDL default clauses:
Exposed also wraps values in JSONB() in DML operations:
SQLite stores this value in its binary JSONB representation. A serializer that expects JSON text cannot decode the raw stored value directly.
To make the value available as JSON text, SQLite provides the JSON() SQL function. By default, Exposed applies this function when it reads a jsonb() column from SQLite.
To disable this behavior, set the castToJsonFormat parameter to false when you define the column:
Exposed ignores castToJsonFormat for databases other than SQLite. To convert an individual JSONB expression to JSON, use .castToJson().
JSON functions
Extract data
Use the .extract() function to extract a value from a JSON expression at a specific path. You can extract the result as JSON or as a scalar value of the specified type.
For example, the following query extracts the project name and selects projects whose language is Kotlin:
For databases that use $ as the JSON path root, Exposed adds it to the generated path expression automatically, so don't include $ in the path you pass to .extract(). For example, in MySQL, pass .name instead of $.name.
Check if data exists
To check whether data exists within a JSON expression, use the .exists() function:
Some databases also support filter expressions and optional variables in JSON paths:
Check if JSON contains an expression
To check whether a JSON expression contains a value, use the .contains() function:
On supported databases, you can also limit the check to a specific JSON path:
Cast data to JSON type
Use the .castToJson() function to cast other supported types, such as JSONB, to JSON:
As shown on the example above, on supported databases, you can also cast a text column that stores valid JSON strings to a serializable class.