OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

A unified distributed database ready for your transactional, analytical, and AI workloads.

DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V4.2.1

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive DemosTraining & Certification
    Company
    About OceanBaseTrust CenterLegalPartnerContact Us
    Follow Us

    © OceanBase 2026. All rights reserved

    Cloud Service AgreementPrivacy PolicySecurity
    Contact Us
    Document Feedback
    1. Documentation Center
    2. OceanBase Database
    3. SQL
    4. V4.2.1
    iconOceanBase Database
    SQL - V 4.2.1
    SQL
    KV
    • V 4.4.2
    • V 4.3.5
    • V 4.3.3
    • V 4.3.1
    • V 4.3.0
    • V 4.2.5
    • V 4.2.2
    • V 4.2.1
    • V 4.2.0
    • V 4.1.0
    • V 4.0.0
    • V 3.1.4 and earlier

    JSON_ARRAYAGG

    Last Updated:2023-12-25 08:49:42  Updated
    share
    What is on this page
    Purpose
    Syntax
    Parameters
    Examples

    folded

    share

    Purpose

    JSON_ARRAYAGG() is an aggregate function. It converts the data of all columns to JSON data and returns a single JSON array that contains those JSON values

    Syntax

    JSON_ARRAYAGG(expr [FORMAT JSON]
                       [ORDER SIBLINGS BY order_condition]
                       [ABSENT|NULL ON NULL,]
                       [RETURNING  CLOB|BLOB|JSON|VARCHAR2|VARCHAR2[size],]
                       [STRICT]);
    

    Parameters

    The parameters in the syntax are described as follows:

    • FORMAT JSON: specifies that the return value of the expression is JSON data and no \" will be added to the output result.
    • NULL|ABSENT ON NULL: This clause takes effect only when the calculation result of the expression is NULL.
      • NULL ON NULL: specifies that JSON NULL values will be used as elements of the current array.
      • ABSENT ON NULL: specifies to ignore the array element.
    • STRICT: checks whether the output result is in JSON format.
    • RETURNING CLOB|BLOB|JSON|VARCHAR2|VARCHAR2[size]: specifies the return value type. VARCHAR2[size] specifies the length of the return value.
    • ORDER SIBLINGS BY: sorts the JSON array elements in the return value. This clause is an ORDERR BY clause.

    Examples

    # Use default arguments.
    obclient> CREATE TABLE id_table (id NUMBER);
    obclient> INSERT INTO id_table VALUES(624);
    obclient> INSERT INTO id_table VALUES(null);
    obclient> INSERT INTO id_table VALUES(925);
    obclient> INSERT INTO id_table VALUES(585);
    obclient> SELECT JSON_ARRAYAGG(id) ID_NUMBERS FROM id_table;
    +---------------+
    | ID_NUMBERS    |
    +---------------+
    | [624,925,585] |
    +---------------+
    1 row in set
    
    # Use the FORMAT JSON clause.
    obclient> CREATE TABLE json_table (json_doc VARCHAR2(100))
    obclient> INSERT INTO json_table VALUES('[1]');
    obclient> INSERT INTO json_table VALUES('[1,2]');
    obclient> INSERT INTO json_table VALUES('[1,2,3]')
    obclient> INSERT INTO json_table VALUES('{key:"value"}');
    obclient> SELECT JSON_ARRAYAGG(json_doc FORMAT JSON) FROM json_table;
    +-----------------------------------+
    | JSON_ARRAYAGG(JSON_DOCFORMATJSON) |
    +-----------------------------------+
    | [[1],[1,2],[1,2,3],{key:"value"}] |
    +-----------------------------------+
    1 row in set
    
    # Use the ABSENT ON NULL clause.
    obclient> SELECT JSON_ARRAYAGG(id ORDER BY id ABSENT ON NULL) FROM id_table;
    +----------------------------------------+
    | JSON_ARRAYAGG(IDORDERBYIDABSENTONNULL) |
    +----------------------------------------+
    | [585,624,925]                          |
    +----------------------------------------+
    1 row in set
    
    # Use the ORDER BY clause.
    CREATE TABLE json_table (id NUMBER, text_data VARCHAR2(10), json_data JSON);
    INSERT INTO json_table VALUES(624, 'test1', '[1]');
    INSERT INTO json_table VALUES(null, 'test4', '[2]');
    INSERT INTO json_table VALUES(925, 'test2', '[3]');
    INSERT INTO json_table VALUES(585, 'test5', '[4]');
    INSERT INTO json_table VALUES(585, 'test3', '[5]');
    obclient> SELECT JSON_ARRAYAGG(text_data ORDER BY text_data) FROM json_table;
    +-------------------------------------------+
    | JSON_ARRAYAGG(TEXT_DATAORDERBYTEXT_DATA)  |
    +-------------------------------------------+
    | ["test1","test2","test3","test4","test5"] |
    +-------------------------------------------+
    1 row in set
    
    obclient> SELECT JSON_ARRAYAGG(text_data ORDER BY id) FROM json_table;
    +-------------------------------------------+
    | JSON_ARRAYAGG(TEXT_DATAORDERBYID)         |
    +-------------------------------------------+
    | ["test5","test3","test1","test2","test4"] |
    +-------------------------------------------+
    1 row in set
    
    obclient> SELECT JSON_ARRAYAGG(text_data ORDER BY id,text_data) FROM json_table;
    +---------------------------------------------+
    | JSON_ARRAYAGG(TEXT_DATAORDERBYID,TEXT_DATA) |
    +---------------------------------------------+
    | ["test3","test5","test1","test2","test4"]   |
    +---------------------------------------------+
    1 row in set
    
    # Use the STRICT clause.
    CREATE TABLE json_table (json_doc VARCHAR2(100));
    INSERT INTO json_table VALUES('[1]');
    INSERT INTO json_table VALUES('[1,2]');
    INSERT INTO json_table VALUES('[1,2,3]');
    INSERT INTO json_table VALUES('{key:"value"}');
    
    obclient> SELECT JSON_ARRAYAGG(json_doc FORMAT JSON STRICT) ID_NUMBERS FROM json_table;
    ORA-00600: Invalid JSON text.
    obclient> SELECT JSON_ARRAYAGG(json_doc FORMAT JSON) ID_NUMBERS FROM json_table;
    +-----------------------------------+
    | ID_NUMBERS                        |
    +-----------------------------------+
    | [[1],[1,2],[1,2,3],{key:"value"}] |
    +-----------------------------------+
    1 row in set
    
    

    Previous topic

    JSON_OBJECT
    Last

    Next topic

    JSON_OBJECTAGG
    Next
    What is on this page
    Purpose
    Syntax
    Parameters
    Examples