DB2 Scalar functions - JSON_OBJECT
The JSON_OBJECT function generates a JSON object by using the specified key:value pairs. If no key:value pairs are provided, an empty object is returned.
Although the schema for this function is SYSIBM, the function cannot be specified as a qualified name.
The result type of this expression can be any built-in data type, except the following data types (SQLSTATE 42815):
If the generated value is numeric, it cannot be Infinity, NaN, or sNaN (SQLSTATE 22023).
If FORMAT JSON or FORMAT BSON is not specified, and the generated value is not numeric, any special characters (for example, backslash or double quotation marks) within the result string are escaped.
If FORMAT JSON or FORMAT BSON is not specified and the expression is binary string type, it is interpreted as FORMAT BSON.
If JSON-expression is a character string data type, it is treated as JSON data.
If JSON-expression is a binary string data type, it is interpreted as UTF-8 data.
Generating a JSON object with unique keys is considered best practice. If key-expression generates unique key names, omit WITH UNIQUE KEYS to improve performance.
See CREATE TABLE statement for the description of built-in data types.
VALUES JSON_OBJECT(KEY 'first' VALUE 'John', KEY 'last' VALUE 'Doe') 1 ----------------------------- {"first":"John","last":"Doe"}
SELECT JSON_OBJECT(KEY 'Last name' VALUE LASTNAME , KEY 'Hire date' VALUE HIREDATE , KEY 'Salary' VALUE SALARY) FROM EMPLOYEE WHERE EMPNO = '000020'; 1 ------------------------------------------------------------------- {"Last name":"THOMPSON","Hire date":"1973-10-10","Salary":41250.00}