json_typeof() 是一个系统函数,它将顶层json值 的类型作为文本字符串返回。
json_typeof() 添加于 PostgreSQL 9.4。
用法
json_typeof (json) →text
将返回以下值之一:
objectarraystringnumberbooleannull
请注意,这些值,特别是在null的情况下,与相应的SQL数据类型并非直接等价。
变更历史
- PostgreSQL 9.4
- 添加于(提交 4d212bac)
示例
使用 json_typeof() 来确定各种 json 值的类型
postgres=# SELECT json_typeof('{"foo":[]}'::json);
json_typeof
-------------
object
(1 row)
postgres=# SELECT json_typeof('[]'::json);
json_typeof
-------------
array
(1 row)
postgres=# SELECT json_typeof('"foo"'::json);
json_typeof
-------------
string
(1 row)
postgres=# SELECT json_typeof('123'::json);
json_typeof
-------------
number
(1 row)
postgres=# SELECT json_typeof('false'::json);
json_typeof
-------------
boolean
(1 row)
postgres=# SELECT json_typeof('null'::json);
json_typeof
-------------
null
(1 row)
请注意,任何数字值都映射到 number 类型
postgres=# SELECT json_typeof(123::int::text::json); json_typeof ------------- number (1 row) postgres=# SELECT json_typeof(1.23::numeric::text::json); json_typeof ------------- number (1 row)
一个 json 类型的 null 不等同于 SQL 的 NULL
postgres=# SELECT json_typeof('null'::json),
json_typeof(NULL::json) IS NULL;
json_typeof | ?column?
-------------+----------
null | t
(1 row)
参考资料
- PostgreSQL 文档: JSON 处理函数
