Purpose
JSON_VALUE() extracts a value in a JSON document from the specified path and returns the extracted value. You can also set the function to convert the extracted value to the desired 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
Description
Parameters
The parameters are described as follows:
json_docspecifies the JSON document.pathis a JSON path that points to a location in the document, and must be a string.typesupports the following data types:FLOATDOUBLEDECIMALSIGNEDUNSIGNEDDATETIMEDATETIMEYEAR(Note thatYEAR(1)andYEAR(2)are not supported.)CHARJSON
If you do not use a
RETURNINGclause, the data type of the return value of the function isVARCHAR(512). If you do not specify the character set for the return type,JSON_VALUE()uses the utf8mb4 character set and a binary collation, and the return value is case-sensitive. If you specify the utf8mb4 character set for the result, the server uses the default collation for the character set, and the return value is case-insensitive.You can specify the
on_emptyclause to determine the behavior ofJSON_VALUE()when no data is found at the specified path. The supported values of this clause are as follows:NULL ON EMPTY: specifies thatJSON_VALUE()returnsNULL. This is the default value ofON EMPTY.DEFAULT value ON EMPTY: returns the specifiedvaluewhose type must match the value of the return type.ERROR ON EMPTY: returns an error.
When an error occurs,
on_errortakes one of the following values:NULL ON ERROR: JSON_VALUE() returnsNULL. If you do not specify theON ERRORclause, this is the default behavior.DEFAULT value ON ERROR: returns the specifiedvaluewhose type must match the value of the return type.ERROR ON ERROR: returns an error.
Notice
When you specify the
ON EMPTYclause, you must put it before allON ERRORclauses.
Error handling
JSON_VALUE() checks the validity of all JSON inputs of documents and paths. If an input is invalid, an SQL error is returned without triggering the ON ERROR clause.
The ON ERROR clause is triggered if one of the following events occurs:
When you try to extract an object or array, the specified path is parsed into multiple paths in the JSON document.
A conversion error occurs when you, for example, try to convert
'asdf'to anUNSIGNEDvalue.Data is truncated.
Even if you specify NULL ON ERROR or DEFAULT ... ON ERROR, a conversion error always triggers an alert.
When the source JSON document specified for json_doc contains no data at the specified path, the ON EMPTY clause is triggered.
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