SQL JSON_ARRAY function
JSON_ARRAY(1, 'abc', NULL) is [1, "abc", null] on MySQL and SQLite but [1, "abc"] on SQL Server, Oracle and PostgreSQL, which drop NULLs unless told otherwise.
- 类别
- Strings
- Returns
JSON text- In this category
- 144 of 172
语法
JSON_ARRAY(value [, value ...] [NULL ON NULL | ABSENT ON NULL])
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| SQL Server | 是 | SQL Server 2022+ |
| PostgreSQL | 是 | JSON_ARRAY constructor on 16+; jsonb_build_array earlier |
| MySQL | 是 | Supported under this name. |
| SQLite | 是 | Supported under this name. |
| Oracle | 是 | 12.2+ |
示例
MySQL
SELECT JSON_ARRAY(1, 'abc', NULL) AS arr;
SQLite
SELECT json_array(1, 2, 3) AS arr;
SQL Server
SELECT JSON_ARRAY(city, country) AS dest FROM trips;
值得了解
- NULL handling follows the standard on SQL Server, Oracle and PostgreSQL: ABSENT ON NULL is the default, so NULLs vanish. Add NULL ON NULL to keep them. MySQL and SQLite always keep them as null.
- Arguments come from one row. Nest constructors freely, JSON_ARRAY(1, JSON_OBJECT(...)), but collecting values across rows takes an aggregate: JSON_ARRAYAGG, json_agg or json_group_array.
- The result type differs: JSON on MySQL, json on PostgreSQL (jsonb with RETURNING jsonb), nvarchar on SQL Server, text on SQLite, VARCHAR2 on Oracle.
Questions about JSON_ARRAY
How do I build an array from multiple rows?
Aggregate instead of construct: JSON_ARRAYAGG (MySQL, Oracle, SQL Server 2025), json_agg/jsonb_agg (PostgreSQL), json_group_array (SQLite), FOR JSON on older SQL Server. JSON_ARRAY only wraps the values you hand it.
Why did my NULLs disappear?
SQL Server, Oracle and PostgreSQL default to ABSENT ON NULL. Write JSON_ARRAY(a, b NULL ON NULL) to keep them. On MySQL and SQLite NULLs are always kept, so wrap the value in a CASE or filter it out first.
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, SQLite, 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.