检查 json 属性的类型

Meh*_*ran 1 json postgresql-9.3

我正在寻找一种方法来验证 PostgreSQL 中 json 变量的类型。这就是我的意思:

get_type('"s"'::josn)  => 'string'
get_type('1'::json)    => 'number'
get_type('2.3'::json)  => 'number'
get_type('null'::json) => 'null'
get_type('[]'::json)   => 'array'
get_type('{}'::json)   => 'object'
Run Code Online (Sandbox Code Playgroud)

我正在寻找该get_type功能。

Cra*_*ger 6

您可以使用json_typeof(或jsonb_typeof用于 9.4's jsonb)。尽管9.3 文档缺少它,但它显然存在,并且记录在 9.4 及更高版本中的 json 函数手册中