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