SQL JSON_VALUE function
JSON_VALUE(doc, '$.user.name') reads one scalar from a JSON document as SQL text. It is the usual way to index, filter or cast a single JSON field.
- Категория
- Strings
- Returns
text- In this category
- 139 of 172
Синтаксис
JSON_VALUE(expression, path)
Поддержка движка
| Движок | Support | Заметки |
|---|---|---|
| SQL Server | Да | Supported under this name. |
| PostgreSQL | Different name or syntax | PostgreSQL 17+; the ->> operator before that |
| MySQL | Да | MySQL 8.0.21+, with an optional RETURNING type |
| SQLite | Not built in | no JSON_VALUE; json_extract unwraps to text already |
| Oracle | Да | Supported under this name. |
Примеры
SQL Server
SELECT JSON_VALUE(config, '$.region') AS region FROM settings;
Oracle
SELECT JSON_VALUE(doc, '$.user.name') AS name FROM payloads;
SQL Server
SELECT JSON_VALUE(config, 'strict $.owner.email') AS email FROM settings;
Полезно знать
- JSON_VALUE is scalar-only: pointed at an object or array it returns NULL by default. SQL Server raises an error instead with a 'strict $...' path, Oracle and PostgreSQL with ERROR ON ERROR. A silent NULL usually means you asked for structure; use JSON_QUERY.
- The result is text (nvarchar(4000) on SQL Server). Cast it for arithmetic, TRY_CAST(JSON_VALUE(...) AS int), or declare the type with RETURNING on MySQL, Oracle and PostgreSQL.
- Malformed JSON behaves differently: SQL Server and PostgreSQL raise an error even in lax mode, while Oracle returns NULL by default. Validate on load with ISJSON, IS JSON or a CHECK constraint.
Questions about JSON_VALUE
JSON_VALUE or JSON_QUERY?
JSON_VALUE for scalars (strings, numbers, booleans); JSON_QUERY for objects and arrays. JSON_VALUE returns NULL for an object or array by default; JSON_QUERY on a scalar returns NULL on SQL Server and the quoted JSON scalar on PostgreSQL and Oracle 23ai.
Why does JSON_VALUE return NULL on my array?
Lax mode hides the type error: a path into an object or array yields NULL. Switch to JSON_QUERY for the fragment, or use 'strict $...' to make the mistake loud.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.
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.