Skip to main content

JSON_ARRAY

Introduced or updated: v1.2.98

Creates a JSON array with specified values.

Syntax

JSON_ARRAY(value1[, value2[, ...]])

Return Type

JSON array.

Examples

Example 1: Creating JSON Array with Constant Values or Expressions

SELECT JSON_ARRAY('Databend', 3.14, NOW(), TRUE, NULL);

json_array('databend', 3.14, now(), true, null) |
--------------------------------------------------------+
["Databend",3.14,"2023-09-06 07:23:55.399070",true,null]|

SELECT JSON_ARRAY('fruits', JSON_ARRAY('apple', 'banana', 'orange'), JSON_OBJECT('price', 1.2, 'quantity', 3));

json_array('fruits', json_array('apple', 'banana', 'orange'), json_object('price', 1.2, 'quantity', 3))|
-------------------------------------------------------------------------------------------------------+
["fruits",["apple","banana","orange"],{"price":1.2,"quantity":3}] |

Example 2: Creating JSON Array from Table Data

CREATE TABLE products (
ProductName VARCHAR(255),
Price DECIMAL(10, 2)
);

INSERT INTO products (ProductName, Price)
VALUES
('Apple', 1.2),
('Banana', 0.5),
('Orange', 0.8);

SELECT JSON_ARRAY(ProductName, Price) FROM products;

json_array(productname, price)|
------------------------------+
["Apple",1.2] |
["Banana",0.5] |
["Orange",0.8] |
Did this page help you?
Yes
No