brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: the ->> operator reads a value from JSON in SQLite without json_extract

Sqlite
~/tips/tip-sqlite-json-arrow-operator

SELECT prefs ->> 'theme', prefs -> 'theme', prefs ->> '$.lang'

Pulls fields out of a JSON column with short arrow syntax instead of function calls.

Plenty of apps store settings as a JSON string in a text column. Since SQLite 3.38 you can read from it with the same arrows PostgreSQL and MySQL use.

SELECT prefs ->> 'theme', prefs -> 'theme', prefs ->> '$.lang'
FROM users;
dark|"dark"|nl

The double arrow ->> returns a plain SQL value: text, a number or NULL, ready to compare or sort. The single arrow -> returns JSON, so the same string comes back with quotes around it. That’s what you want when you pass the piece on to another JSON function, and the wrong thing in a WHERE.

On the right you can give a key name, a full path like '$.address.city', or a number for an array index. WHERE prefs ->> 'theme' = 'dark' works as a filter.

A missing key gives NULL, not an error, so a typo in a key name quietly returns nothing. With an empty result, that’s the first thing I’d check.

A filter on a JSON value reads every row, since there is no index on the inside of the text. If you filter on one key a lot, an index on the expression helps: CREATE INDEX users_theme ON users(prefs ->> 'theme');. The query then has to use exactly the same expression. On a SQLite older than 3.38 the arrows are a syntax error, and you are back to json_extract().

Further reading: SQLite JSON functions and operators

More Sqlite tips SELECT unixepoch('2026-01-01 00:00:00'); SELECT name, iif(qty > 0, 'in stock', 'sold out') AS state sqlite3 -bail app.db < migrate.sql CREATE INDEX 'orders_user_id' ON 'orders'('user_id'); –> users(id) CREATE INDEX users_active_email ON users(email) WHERE active = 1; All Sqlite tips (27) All topics

Link