jsonb_typeof()

返回 jsonb 值类型的字符串的函数

jsonb_typeof() 是一个系统函数,它将顶级 jsonb 值 的类型以文本字符串的形式返回。

jsonb_typeof() 已在 PostgreSQL 9.4 中添加。

用法

json_typeof ( jsonb ) → text

将返回以下值之一:

  • object
  • array
  • string
  • number
  • boolean
  • null

请注意,这些值,特别是 null 的情况,与相应的 SQL 数据类型并不完全等价。

变更历史

示例

使用 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)

一个 jsonbnull 不等同于 SQL 的 NULL

postgres=# SELECT jsonb_typeof('null'::jsonb),
                  jsonb_typeof(NULL::jsonb) IS NULL;
 jsonb_typeof | ?column?
--------------+----------
 null         | t
(1 row)

分类

JSON, 系统函数

另请参阅

json_typeof()

反馈

提交任何关于“jsonb_typeof()”的评论、建议或更正,请点击 此处