// strings reference

SQL json_each function

FROM json_each(config) gives one row per member or element, with key, value, type and path columns. Joined to a table, it is how SQLite unnests JSON arrays and objects.

카테고리
Strings
Returns
rowset
In this category
156 of 172

구문

json_each(json [, path])
json_tree(json [, path])

Each block is a separate form. The support table and the examples show which engine takes which.

엔진 지원

엔진Support메모
SQL Server Not built in OPENJSON covers it
PostgreSQL Different name or syntax json_each / jsonb_each for objects, jsonb_array_elements for arrays
MySQL Not built in JSON_TABLE covers it
SQLite 예 Supported under this name.
Oracle Not built in JSON_TABLE covers it

예시

SQLite

SELECT key, value FROM json_each('{"a": 1, "b": 2}');

SQLite

SELECT je.key, je.value
FROM settings s, json_each(s.config) je;

SQLite

SELECT jt.fullkey, jt.value
FROM events e, json_tree(e.payload) jt;

알아 두면 좋은 정보

  • json_each walks one level: members of an object or elements of an array. Nested structure arrives as whole JSON values; json_tree recurses the whole subtree with fullkey paths.
  • The columns are key, value, type, atom, id, parent and fullkey; join it against the source table with a comma (cross join) or an explicit JOIN for correlation.
  • PostgreSQL's jsonb_each works on objects only and errors on an array; arrays need jsonb_array_elements. SQLite's json_each takes either.

Questions about json_each

How do I count JSON array items per row?

Join and group: SELECT o.id, count(*) FROM orders o, json_each(o.items) GROUP BY o.id. One row per element makes counting a plain aggregate.

json_each or json_tree?

json_each for the top level of an array or object; json_tree when you need every nested value with its path. Tree output includes each container too, not just leaves.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: PostgreSQL, SQLite.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.