// strings reference

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.

Categoría
Strings
Returns
JSON text
In this category
144 of 172

Sintaxis

JSON_ARRAY(value [, value ...] [NULL ON NULL | ABSENT ON NULL])

Soporte del motor

MotorSupportNotas
SQL Server Sí SQL Server 2022+
PostgreSQL Sí JSON_ARRAY constructor on 16+; jsonb_build_array earlier
MySQL Sí Supported under this name.
SQLite Sí Supported under this name.
Oracle Sí 12.2+

Ejemplos

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;

Conviene saberlo

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

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, 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.