JSON_VALID()
This page is part of MariaDB's Documentation.
The parent of this page is: Functions for MariaDB Xpand
Topics on this page:
Overview
Returns a boolean value to indicate if a string is valid JSON data.
USAGE
JSON_VALID(json_data)
Argument Name | Description |
---|---|
| The JSON data to check |
DETAILS
JSON_VALID()
is a JSON function that validates the syntax of JSON data.
JSON data is valid if it conforms to ECMA-404: The JSON Data Interchange Standard.
If the argument is valid JSON data a 1
is returned; if it is invalid JSON data, a 0
is returned; if it is a NULL
, NULL
is returned.
EXAMPLES
With Literal Values
In the following example, JSON_VALID()
is called with literal values:
SELECT JSON_VALID('{"bar": "a string"}') AS out_1,
JSON_VALID('[a,1]') AS out_2,
JSON_VALID(5) AS out_3;
+-------+-------+-------+
| out_1 | out_2 | out_3 |
+-------+-------+-------+
| 1 | 0 | 1 |
+-------+-------+-------+
Example Schema and Data
Some of the examples are based on the contacts
table. We are using a TEXT
field instead of a JSON
field so that we can insert invalid data:
CREATE TABLE valid_test (
example TEXT
);
INSERT INTO valid_test VALUES
('{"foo": 42, "bar": "hi"}'),
('{4: 5}'),
(NULL);
Per-row Values
SELECT JSON_VALID(example) AS is_valid
FROM valid_test;
+----------+
| is_valid |
+----------+
| 1 |
| 0 |
| NULL |
+----------+