JSON data (Daryl - Live)

Moved to PR#270arrow-up-right. See previewarrow-up-right.

The content has been updated since!


CrateDB combines the flexibility of NoSQL document stores with the power of SQL. It enables you to store, query, and index semi-structured JSON data using standard SQL, making it an excellent choice for applications that handle diverse or evolving schemas.

CrateDB’s support for dynamic objects, nested structures, and dot-notation querying brings the best of both relational and document-based data modeling—without leaving the SQL world.


Object (JSON) Columns

CrateDB allows you to define object columns that can store JSON-style data structures.

CREATE TABLE events (
  id UUID PRIMARY KEY,
  timestamp TIMESTAMP,
  payload OBJECT(DYNAMIC)
);

This allows inserting flexible, nested JSON data into payload:

{
  "user": {
    "id": 42,
    "name": "Alice"
  },
  "action": "login",
  "device": {
    "type": "mobile",
    "os": "iOS"
  }
}

Column Policy: Strict vs Dynamic

You can control how CrateDB handles unexpected fields in an object column:

Column Policy
Behavior

DYNAMIC

New fields are automatically added to the schema at runtime

STRICT

Only explicitly defined fields are allowed

IGNORED

Extra fields are stored but not indexed or queryable

Example with explicitly defined fields:


Querying JSON Fields

Use bracket notation to access nested fields:

CrateDB also supports filtering, sorting, and aggregations on nested values:

circle-info

Dot-notation works for both explicitly and dynamically added fields.


Querying DYNAMIC OBJECTs

To support querying DYNAMIC OBJECTs using SQL, where keys may not exist within an OBJECT, CrateDB provides the error_on_unknown_object_keyarrow-up-right session setting. It controls the behaviour when querying unknown object keys to dynamic objects.

By default, CrateDB will raise an error if any of the queried object keys are unknown. When adjusting this setting to false, it will return NULL as the value of the corresponding key.


Arrays of OBJECTs

Store arrays of objects for multi-valued nested data:

Query nested arrays with filters:

You can also filter by object array fields:


Combining Structured & Semi-Structured Data

CrateDB supports hybrid schemas, mixing standard columns with JSON fields:

This allows you to:

  • Query by fixed attributes (log_level)

  • Flexibly store structured or unstructured metadata

  • Add new fields on the fly without migrations


Indexing Behavior

CrateDB automatically indexes object fields if:

  • Column policy is DYNAMIC

  • Field type can be inferred at insert time

You can also explicitly define and index object fields:

To exclude fields from indexing, set:

circle-info

Too many dynamic fields can lead to schema explosion. Use STRICT or IGNORED if needed.


Aggregating JSON Fields

CrateDB allows full SQL-style aggregations on nested fields:

CrateDB also supports GROUP BY, HAVING, and window functions on object fields.


Further Learning & Resources

Last updated