Purpose
This function extracts a value from a JSON document at the specified path and returns the extracted value, which can be optionally converted to the required data type.
Syntax
JSON_VALUE(json_doc, path [RETURNING type] [on_empty] [on_error])
on_empty:
{NULL | ERROR | DEFAULT value} ON EMPTY
on_error:
{NULL | ERROR | DEFAULT value} ON ERROR
Considerations
Parameters
The following table describes the parameters.
json_doc: specifies a valid JSON document.path: specifies a JSON path to a location in the document. This parameter is a string.type: specifies a data type. Valid values are as follows:FLOATDOUBLEDECIMALSIGNEDUNSIGNEDDATETIMEDATETIMEYEAR(does not supportYEAR(1)andYEAR(2))CHARJSON
If the
RETURNINGclause is not specified, the return type of this function isVARCHAR(512). When no character set is specified for the return type,JSON_VALUE()uses the utf8mb4 character set and binary collation, and is case-sensitive. If you specify utf8mb4 as the character set for the result, the server uses the default collation for this character set and is not case-sensitive.The
on_emptyclause specifies the behavior ofJSON_VALUE()when no data is found at the specified path. Valid values are as follows:NULL ON EMPTY:JSON_VALUE()returnsNULL. This is the default behavior forON EMPTY.DEFAULT value ON EMPTY: returns the providedvalue, which must be of the same type as the return type.ERROR ON EMPTY: the function throws an error.
The
on_errorclause specifies the behavior ofJSON_VALUE()when an error occurs. Valid values are as follows:NULL ON ERROR:JSON_VALUE()returnsNULL. This is the default behavior when theON ERRORclause is not specified.DEFAULT value ON ERROR: returns the providedvalue, which must be of the same type as the return type.ERROR ON ERROR: throws an error.
Notice
If the
ON EMPTYclause is used, it must appear before allON ERRORclauses. An incorrect order will result in a syntax error.
Error handling
By default, JSON_VALUE() checks the validity of all JSON inputs (the document and path). If any input is invalid, it throws an SQL error without triggering the ON ERROR clause.
The ON ERROR clause is triggered in the following cases:
The input path resolves to multiple paths in the JSON document when extracting an object or array.
A conversion error occurs. For example, when you try to convert
'asdf'to anUNSIGNEDvalue.Data truncation occurs.
Even if you specify NULL ON ERROR or DEFAULT ... ON ERROR, a conversion error will always trigger a warning.
The ON EMPTY clause is triggered when the source JSON document (json_doc) does not contain any data at the specified location (path).
Examples
obclient> SELECT JSON_VALUE('{"fname": "Smith", "lname": "Will"}', '$.fname');
+--------------------------------------------------------------+
| JSON_VALUE('{"fname": "Smith", "lname": "Will"}', '$.fname') |
+--------------------------------------------------------------+
| Smith |
+--------------------------------------------------------------+
1 row in set
obclient> SELECT JSON_VALUE('{"item": "shoes", "price": "69.73"}', '$.price'
RETURNING DECIMAL(4,2)) AS price;
+-------+
| price |
+-------+
| 69.73 |
+-------+
1 row in set
