json_typeof()

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

json_typeof() 是一个系统函数,它将顶层json值 的类型作为文本字符串返回。

json_typeof() 添加于 PostgreSQL 9.4

用法

json_typeof ( json ) → text

将返回以下值之一:

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

请注意,这些值,特别是在null的情况下,与相应的SQL数据类型并非直接等价。

变更历史

示例

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

分类

JSON, 系统函数

另请参阅

jsonb_typeof()

反馈

提交有关“json_typeof()”的任何评论、建议或更正 请在此处