// strings reference

SQL JSON_QUERY function

JSON_QUERY(doc, '$.tags') returns the whole tags array as JSON text, where JSON_VALUE on the same path returns NULL.

Categoria
Strings
Returns
JSON text
In this category
140 of 172

Sintaxe

JSON_QUERY(expression, path)

Suporte a mecanismos

MotorSupportNotas
SQL Server Sim Supported under this name.
PostgreSQL Different name or syntax PostgreSQL 17+; -> and ->> before that
MySQL Not built in no JSON_QUERY; JSON_EXTRACT plays this role
SQLite Not built in json_extract returns fragments too
Oracle Sim Supported under this name.

Exemplos

SQL Server

SELECT JSON_QUERY(config, '$.tags') AS tags
FROM settings;

PostgreSQL

SELECT JSON_QUERY(config, '$.items[*].sku' WITH WRAPPER) AS skus
FROM settings;

Bom saber

  • Scalars differ by engine: SQL Server's JSON_QUERY returns NULL for a scalar path in lax mode, while PostgreSQL and Oracle 23ai return it as JSON text ('"x"', quotes included). WITH WRAPPER turns it into a one-element array.
  • Output is JSON text in the engine's own formatting, so whitespace and key order can differ between engines. Compare results as parsed JSON, not as strings.
  • A wildcard path matching several values needs a wrapper: JSON_QUERY(doc, '$.items[*].sku' WITH ARRAY WRAPPER) collects them into one array on Oracle, PostgreSQL and SQL Server 2025.

Questions about JSON_QUERY

How do I get an array as rows?

Shred it: OPENJSON on SQL Server, JSON_TABLE on Oracle and MySQL, json_array_elements on PostgreSQL, json_each on SQLite. JSON_QUERY extracts; the rowset functions flatten.

Why does JSON_QUERY return NULL for a simple string?

On SQL Server it only returns objects and arrays, so a scalar path gives NULL in lax mode and an error in strict mode. Use JSON_VALUE for scalars.

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: SQL Server, PostgreSQL, Oracle.

// 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.