
JSON_VALUE
The JSON_VALUE function returns an SQL scalar value from a JSON text by using an SQL/JSON path expression.
- JSON-expression
- An expression that returns a value that is a built-in string data type. If a character or graphic value is returned, it must contain correctly formatted JSON data. If it is a binary data type, it is interpreted according to the explicit or implicit FORMAT clause.
- FORMAT JSON or FORMAT BSON
- Specifies how JSON-expression is to be interpreted.
- FORMAT JSON
- JSON-expression contains JSON data. If JSON-expression is binary data, the data is interpreted as UTF-8 or UTF-16. Binary data cannot be encoded using an EBCDIC CCSID.
- FORMAT BSON
- JSON-expression contains the BSON representation of JSON data. When FORMAT BSON is specified, JSON-expression must be a binary string data type.
- sql-json-path-expression
- An expression that returns a value that is a built-in character or graphic string data type. The
string is interpreted as an SQL/JSON path expression and is used to locate a JSON value within the
JSON data specified by JSON-expression.
If more than one value has the same key, one of the JSON values will be selected.
- AS path-name
- Specifies a name to be used to identify the sql-json-path-expression.
- RETURNING data-type
- Specifies the data type of the result. For CHAR and VARCHAR results, the CCSID cannot be 65535. The default is CLOB(2G) CCSID 1208.
- ON EMPTY
- Specifies the behavior when an empty sequence is found using sql-json-path-expression.
- NULL ON EMPTY
- A null value is returned. This is the default.
- ERROR ON EMPTY
- An error is returned.
- DEFAULT default-expression ON EMPTY
- The value specified by default-expression is returned. The expression must be assignment compatible to the result data type.
- ON ERROR
- Specifies the behavior when an error is encountered by JSON_VALUE.
- NULL ON ERROR
- A null value is returned. This is the default.
- ERROR ON ERROR
- An error is returned.
- DEFAULT default-expression ON ERROR
- The value specified by default-expression is returned. The expression must be assignment compatible to the result data type.
The result can be null. If JSON-expression is null, the result is the null value.
Example
- Return a value from a JSON text as an
integer.
The result is 987.VALUES (JSON_VALUE('{"id":"987"}', '$.id' RETURNING INTEGER));
- Try to return a value from a JSON text that is an array value.
Handle any error by returning a default string.
The result is Not found since the value corresponding to the friends key is an array, not a scalar value.VALUES (JSON_VALUE('{"friends":["John","Lisa"]}', 'strict $.friends' DEFAULT 'Not found' ON ERROR));
