JSON_SERIALIZE
The JSON_SERIALIZE function takes JSON data of SQL data type VARCHAR2, NVARCHAR2, CLOB, NCLOB, JSON, or BLOB as input and returns a textual representation of it. You typically use the JSON_SERIALIZE function to transform the result of a query.
Syntax
JSON_SERIALIZE(json_expr [JSON_SERIALIZE_return_clause] [PRETTY] [ASCII] [TRUNCATE][JSON_SERIALIZE_on_error])
JSON_SERIALIZE_return_clause::= RETURNING {BLOB|CLOB|NCLOB|NVARCHAR2 [(size)]|[VARCHAR2 [(size [BYTE|CHAR])]}
JSON_SERIALIZE_on_error::= {NULL|ERROR|EMPTY [empty_clause]} ON ERROR
empty_clause::= ARRAY|OBJECTParameters
JSON_SERIALIZE has the parameters:
| Parameter | Description |
|---|---|
|
|
Input to the |
|
|
Optional clause that allows you to specify the return type of the output. The default is The supported return types are:
|
|
|
Optional clause that enables human readability formatting by including line breaks and indentation. |
|
|
Optional clause that enables non-ASCII characters to be output using JSON escape sequences. |
|
|
Optional clause that handles cases where the output is too large to fit into the buffer of the specified (or default) return type. If the output is too large to fit into the return type, the value is truncated to fit into the buffer of the return type. |
|
|
Optional clause that enables error handling control. If not specified, the default is Valid clauses are |
|
|
If there is an error, and |
|
|
If there is an error, and This is the default. |
|
|
If there is an error, and the Valid options for the
empty_clause are:
|
Description
-
The
JSON_SERIALIZEfunction takes JSON data of various SQL types as input and returns a textual representation of it. -
You can use the
JSON_SERIALIZEfunction to convert binary JSON data to textual form or to transform textual JSON data by pretty-printing it or escaping non-ASCII unicode characters in it. -
You can specify the
JSON_SERIALIZE_return_clause,JSON_SERIALIZE_on_error,PRETTY,ASCII, andTRUNCATEoptional clauses in any order. -
If you specify the
TRUNCATEclause and theJSON_SERIALIZE_on_errorclause, and if the value is too large to fit into the buffer of the return type,JSON_SERIALIZEtruncates the value to fit into the buffer and does not raise an error. For example, if you specifyTRUNCATEandERRORONERROR,JSON_SERIALIZEtruncates the value and does not return an error.
Examples
The following examples illustrate various uses of the JSON_SERIALIZE function.
The examples use ttIsql and are run from the ttIsql Command> prompt.
JSON.CREATE TABLE json_doc (id TT_INTEGER, data JSON);
INSERT INTO json_doc values (1, '{"id":1,"first_name":"John","last_name":"Smith"}');1 row inserted.JSON_SERIALIZE function to convert the data of type JSON to textual form. Since there is no RETURNING clause, the default return type is VARCHAR2 (4000).SELECT JSON_SERIALIZE (data) FROM json_doc;The output is the following:
< {"id":1,"first_name":"John","last_name":"Smith"} >
1 row found.Examples Using the Optional Clauses
Let's experiment with the PRETTY clause.
Let's first omit it.
SELECT JSON_SERIALIZE ('{"id":1,"first_name":"John","last_name":"Smith"}');The output is the following:
< {"id":1,"first_name":"John","last_name":"Smith"} >
1 row found.PRETTY keyword, which pretty-prints the output in human readable form with line breaks and indentation.SELECT JSON_SERIALIZE ('{"id":1,"first_name":"John","last_name":"Smith"}' PRETTY);< {
"id" : 1,
"first_name" : "John",
"last_name" : "Smith"
} >
1 row found.RETURNING clause to specify the return type of the output. This example specifies VARCHAR2 (100).SELECT JSON_SERIALIZE ('{"id":1,"first_name":"John","last_name":"Smith"}' RETURNING VARCHAR2 (100));< {"id":1,"first_name":"John","last_name":"Smith"} >
1 row found.VARCHAR2 (100). describe SELECT JSON_SERIALIZE ('{"id":1,"first_name":"John","last_name":"Smith"}' RETURNING VARCHAR2 (100));Prepared Statement:
Columns:
EXP VARCHAR2 (100) NOT NULLTRUNCATE keyword truncates the output to fit into the buffer of the return type. In the following example, since the return type is VARCHAR2(10), the output is truncated to fit into the buffer of a VARCHAR2 (10) return type.SELECT JSON_SERIALIZE ('{"id":1,"first_name":"John","last_name":"Smith"}' TRUNCATE RETURNING VARCHAR2 (10));< {"id":1,"f >
1 row found.BLOB.CREATE TABLE json_doc2 (id TT_INTEGER, data BLOB);
INSERT INTO json_doc2 values (1, '{"id":1,"first_name":"John","last_name":"Smith"}');1 row inserted.SELECT the data to illustrate that the result is of type BLOB.SELECT data FROM json_doc2;< 7B226964223A312C2266697273745F6E616D65223A224A6F686E222C226C6173745F6E616D65223A22536D697468227D >
1 row found.JSON_SERIALIZE function to convert the JSON data of type BLOB to textual form. Let's specify a VARCHAR2 (200) return type.SELECT JSON_SERIALIZE (data RETURNING VARCHAR2 (200)) FROM json_doc2;< {"id":1,"first_name":"John","last_name":"Smith"} >
1 row found.Error Handling Examples
The following examples illustrate various uses of the ON ERROR clause.
Here is an example that has a syntax error. Since the default is ERROR ON ERROR, JSON_SERIALIZE returns an error.
SELECT JSON_SERIALIZE ('This is not JSON!'); 2379: JSON syntax error : JZN-00078: Invalid JSON keyword 'This'
The command failed.ON ERROR clause to NULL ON ERROR. Even though there is a syntax error, JSON_SERIALIZE returns NULL due to the NULL ON ERROR clause.SELECT JSON_SERIALIZE ('This is not JSON!' NULL ON ERROR);< <NULL> >
1 row found.Let's use the same JSON syntax, but change the ON ERROR clause to EMPTY ON ERROR. Even though there is a syntax error, JSON_SERIALIZE returns an empty array due to the EMPTY ON ERROR clause. When using the EMPTY ON ERROR clause, the default is EMPTY ARRAY ON ERROR.
SELECT JSON_SERIALIZE ('This is not JSON!' EMPTY ON ERROR);The output is as follows:
< [] >
1 row found.TRUNCATE keyword with the ERROR ON ERROR clause. If the output is too large to fit into the buffer of the return type, JSON_SERIALIZE truncates the output to fit into the buffer instead of raising an error.SELECT JSON_SERIALIZE ('{"Name" : [1,2,3,4]}' RETURNING VARCHAR2 (4) TRUNCATE ERROR ON ERROR);< {"Na >
1 row found.See Also
- JSON_SERIALIZE Function in the Oracle TimesTen In-Memory Database JSON Developer's Guide.