SQLite
json_type()函数返回一个给定的 JSON 或者 JSON 中指定路径的值的类型。
json_type()
语法
这里是 SQLite
json_type()的语法:
json_type(json_doc)
或者
json_type(json_doc, path)
参数
json_doc必需的。一个 JSON 值。
返回值
json_type()函数返回一个字符串,它代表了给定的 JSON 值的类型。
json_type()函数将返回下面值中的一个:
'object': JSON 对象
'array': JSON 数组
'text': JSON 字符串
'integer': JSON 整数
'real': JSON 浮点数
'true': JSON 值
true
'false': JSON 值
false
'null': JSON 值
null
如果参数为
NULL,此函数将返回
NULL。
如果参数不是有效的 JSON 文档,SQLite 将会给出错误。您可以使用
json_valid()验证 JSON 文档的有效性。
json_type()
示例
这里列出了几个常见的
json_type()用法示例。
示例 1
SELECT json_type('true'), json_type('false'), json_type('null'), json_type('"abc"'); json_type('true') = truejson_type('false') = false json_type('null') = nulljson_type('"abc"') = text示例 2: 数字
SELECT json_type('1'), json_type('1.23'); json_type('1') = integerjson_type('1.23') = real示例 3: 数组
SELECT json_type('[]'), json_type('[1, 2]'); json_type('[]') = arrayjson_type('[1, 2]') = array示例 4: 对象
SELECT json_type('{}'), json_type('{"x": 1}'); json_type('{}') = objectjson_type('{"x": 1}') = object示例 5: 路径
SELECT json_type('{"x": 1}', '$.x'); json_type('{"x": 1}', '$.x') = integer