json_parse_nullable_error_to_value
Descriptionβ
The JSON_PARSE_NULLABLE_ERROR_TO_VALUE
function is used to parse a JSON string into a valid JSON object. If the input JSON string is invalid, it will return the default value specified by the user, instead of throwing an error. If the input is NULL
, it will return the default value.
Syntaxβ
JSON_PARSE_NULLABLE_ERROR_TO_VALUE( <str> , <default_value>)
Aliasesβ
- JSONB_PARSE_NULLABLE_ERROR_TO_VALUE
Required Parametersβ
parameters | described |
---|---|
<str> | The input string in JSON format to be parsed. |
<default_value> | The default value returned when parsing fails. |
Return Valueβ
If the input string is a valid JSON, it returns the corresponding JSON object. If the input string is invalid or NULL, it returns the default value specified by the default_value parameter.
Examplesβ
- Valid JSON string:
SELECT JSON_PARSE_NULLABLE_ERROR_TO_VALUE('{"name": "John", "age": 30}', 'default');
+------------------------------------------------------------------------------+
| JSON_PARSE_NULLABLE_ERROR_TO_VALUE('{"name": "John", "age": 30}', 'default') |
+------------------------------------------------------------------------------+
| {"name": "John", "age": 30} |
+------------------------------------------------------------------------------+
- Invalid JSON string:
SELECT JSON_PARSE_NULLABLE_ERROR_TO_VALUE('{"name": "John", "age": }', 'default');
+----------------------------------------------------------------------------+
| JSON_PARSE_NULLABLE_ERROR_TO_VALUE('{"name": "John", "age": }', 'default') |
+----------------------------------------------------------------------------+
| default |
+----------------------------------------------------------------------------+
- Input is NULL:
SELECT JSON_PARSE_NULLABLE_ERROR_TO_VALUE(NULL, 'default');
+---------------------------------------------------------------+
| JSON_PARSE_NULLABLE_ERROR_TO_VALUE(NULL, 'default') |
+---------------------------------------------------------------+
| default |
+---------------------------------------------------------------+