JSON_ARRAYAGG
The JSON_ARRAYAGG function returns a JSON array that contains an array element for each value in a set of JSON or SQL values.
Note: The schema is SYSIBM. The function name cannot be specified as a qualified name.
- JSON-expression
- The expression to use to generate a value in the JSON array. The result type of this expression
can be any built-in data type. It cannot be CHAR or VARCHAR bit data. It cannot be a user-defined
type that is sourced on any of these data types.
- FORMAT JSON or FORMAT BSON
- Specifies whether the JSON expression is already formatted data.
- FORMAT JSON
- The JSON expression is formatted as JSON data. If the JSON expression is a character or graphic string data type, it is treated as JSON data. If the JSON expression is a binary string data type, it is interpreted as UTF-8 data.
- FORMAT BSON
- The JSON expression is formatted as the BSON representation of JSON data and must be a binary string data type.
- ORDER BY
- Specifies the order of the rows from the same grouping set that is processed in the aggregation. Rows in the same grouping are arbitrarily ordered when the ORDER BY clause is not specified, or if the ORDER BY clause cannot differentiate the order of the sort key value.
- ABSENT ON NULL or NULL ON NULL
- Specifies what to return when an array element that is produced by the JSON expression is the
null value.
- ABSENT ON NULL
- A null array element is not included in the JSON array. This setting is the default.
- NULL ON NULL
- A null array element is included in the JSON array.
- RETURNING data-type
- Specifies the format of the result.
- data-type
- The data type of the result. The default data type is Character Large Object [CLOB (2G)]
- FORMAT JSON
- JSON data is returned as a JSON string.
- ENCODING UTF8
- The encoding to use when data-type is a binary string type. This clause is only allowed for binary string types. The result can be null. If the set of values is empty, the result is the null value.
The result can be null. If the set of values is empty, the result is the null value.
Notes
The JSON_ARRAYAGG aggregate function cannot be used as part of an OLAP specification.
Example
- The following example shows the command syntax for returning a JSON array containing all the
department numbers.
The result is the following JSON array.SELECT JSON_ARRAYAGG(deptno) AS deptlist FROM dept;
DEPT_NAME ------------------------------ ["sales","Procurment","finance","Eng","Design","Labour"] 1 record(s) selected.
- The following example shows the command syntax for returning a JSON array for each department
that contains a list of employees that are assigned to that department.
The result is the following two rows.SELECT deptno, JSON_ARRAYAGG(id) AS employe_id from employe group by deptno
DEPTN EMPLOYE_ID ---------- -------------------------------------- 102 [102001,102002,102003,102004] 103 [103001,103002,103003,103004] 2 record(s) selected.
- The following example shows the command syntax for returning a JSON array for each department
number and its corresponding department
name.
The result is the following JSON array.SELECT num as dept_num, JSON_ARRAYAGG (deptname) as dept_name FROM dept group by num
DEPT_NUM DEPT_NAME ----------- -------------------------------------- 101 ["sales"] 102 ["Procurment"] 103 ["finance"] 104 ["Eng"] 105 ["Design"] 106 ["Labour"] 6 record(s) selected.