Skip to content

Lesson 6 of 9 · 24m

JSON1: JSON Without Losing Relational Shape

Use SQLite JSON functions for flexible payloads while keeping hot query paths indexed, validated, and relational where it matters.

SQLite's JSON support is excellent precisely because it does not force you to choose a document database lifestyle. You can keep relational structure for the data that defines invariants and use JSON for payloads that are genuinely variable. The failure mode is treating a JSON column as a junk drawer and then acting surprised when every query becomes a scan.

Modern JSON functions in practice

The JSON1 extension's functions are built into modern SQLite distributions. Check before relying on them:

SELECT json_extract('{"user":{"id":42,"roles":["admin"]}}', '$.user.id');
SELECT json_array_length('{"items":[1,2,3]}', '$.items');

SQLite also supports operators in many builds:

SELECT payload ->> '$.user.id' AS user_id,
       payload -> '$.user.roles' AS roles
FROM events;

-> returns a JSON representation for objects and arrays; ->> returns SQL scalar values such as text, integer, real, or null. That distinction matters when comparing values or indexing expressions.

Store JSON for variation, not avoidance

Good uses include provider-specific webhook payloads, feature metadata, audit details, import snapshots, and configuration fragments. Poor uses include replacing columns that every query filters on, every invariant depends on, or every foreign key should constrain.

CREATE TABLE webhook_events(
  id INTEGER PRIMARY KEY,
  provider TEXT NOT NULL,
  event_type TEXT NOT NULL,
  received_at INTEGER NOT NULL,
  payload TEXT NOT NULL CHECK (json_valid(payload))
);

The CHECK is cheap insurance that the column contains parseable JSON. It does not prove the payload matches the provider schema; that validation belongs at application boundaries.

Index expressions for hot paths

If a JSON field becomes operationally important, acknowledge that fact in the schema:

CREATE INDEX webhook_events_user
ON webhook_events(json_extract(payload, '$.user.id'));

This makes the dependency visible and measurable. It also creates maintenance cost. When half your indexes are extracts from the same opaque blob, the schema is telling you those fields deserve real columns.

Generated columns make the pattern explicit

SQLite supports generated columns:

ALTER TABLE webhook_events
ADD COLUMN provider_user_id TEXT
GENERATED ALWAYS AS (payload ->> '$.user.id') STORED;

CREATE INDEX webhook_events_provider_user
ON webhook_events(provider_user_id);

Stored generated columns consume space and write time. Virtual generated columns compute on read. Choose based on query pressure and storage cost. The important gain is naming: future readers no longer need to reverse-engineer why a JSON path matters.

Field notes

  • -> and ->> are not interchangeable; type differences affect comparisons and indexes.
  • json_valid checks syntax, not business semantics.
  • Expression indexes reveal hidden query dependencies; use them deliberately.
  • Generated columns are a good halfway step before committing to full schema normalization.
  • JSON arrays with relational meaning usually become join tables eventually.
  • Keep raw payloads for audit and replay, but do not force every hot query through them.

Was this lesson helpful?

Sign in to rate