IS_VALID_JSON 函数
IS_VALID_JSON 函数用于验证 JSON 字符串。如果字符串是格式正确的 JSON 字符串,则该函数返回布尔值 true (t);如果字符串格式不正确,函数将返回 false (f)。要验证 JSON 数组,请使用 IS_VALID_JSON_ARRAY 函数
有关更多信息,请参阅JSON 函数。
语法
is_valid_json('json_string')
Arguments
- json_string
-
计算结果为 JSON 字符串的字符串或表达式。
返回类型
BOOLEAN
示例
以下示例创建一个表并插入 JSON 字符串进行测试。
create table test_json(id int identity(0,1), json_strings varchar); -- Insert valid JSON strings -- insert into test_json(json_strings) values ('{"a":2}'), ('{"a":{"b":{"c":1}}}'), ('{"a": [1,2,"b"]}'); -- Insert invalid JSON strings -- insert into test_json(json_strings)values ('{{}}'), ('{1:"a"}'), ('[1,2,3]');
以下示例将对上一个示例中的字符串进行验证。
select id, json_strings, is_valid_json(json_strings) from test_json order by id; id | json_strings | is_valid_json ---+---------------------+-------------- 0 | {"a":2} | true 2 | {"a":{"b":{"c":1}}} | true 4 | {"a": [1,2,"b"]} | true 6 | {{}} | false 8 | {1:"a"} | false 10 | [1,2,3] | false