Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何查询json列中的空对象?_Json_Postgresql_Types_Jsonb - Fatal编程技术网

如何查询json列中的空对象?

如何查询json列中的空对象?,json,postgresql,types,jsonb,Json,Postgresql,Types,Jsonb,查找某个json列包含空对象{}的所有行。这在JSON数组中是可能的,或者如果我在对象中寻找一个特定的键。但我只想知道这个物体是否是空的。似乎找不到可以执行此操作的操作员 dev=# \d test Table "public.test" Column | Type | Modifiers --------+------+----------- foo | json | dev=# select * from test; foo ---------

查找某个json列包含空对象{}的所有行。这在JSON数组中是可能的,或者如果我在对象中寻找一个特定的键。但我只想知道这个物体是否是空的。似乎找不到可以执行此操作的操作员

 dev=# \d test
     Table "public.test"
  Column | Type | Modifiers
 --------+------+-----------
  foo    | json |

 dev=# select * from test;
    foo
 ---------
  {"a":1}
  {"b":1}
  {}
 (3 rows)

 dev=# select * from test where foo != '{}';
 ERROR:  operator does not exist: json <> unknown
 LINE 1: select * from test where foo != '{}';
                                      ^
 HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
 dev=# select * from test where foo != to_json('{}'::text);
 ERROR:  operator does not exist: json <> json
 LINE 1: select * from test where foo != to_json('{}'::text);
                                      ^
 HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
 dwv=# select * from test where foo != '{}'::json;
 ERROR:  operator does not exist: json <> json
 LINE 1: select * from test where foo != '{}'::json;
                                      ^
 HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
作为一个整体,数据类型json没有相等或不相等运算符,因为很难建立相等。在9.4或以后的PigGrES中考虑,这是可能的。有关dba.SE的更多详细信息,请参见上一章:

选择不同的json\u列。。。或GROUP BY json_列失败,原因与无相等运算符相同

将表达式的两侧强制转换为text允许使用=或运算符,但这通常是不可靠的,因为对于相同的JSON值有许多可能的文本表示。在Postgres 9.4或更高版本中,改用jsonb。或者首先使用jsonb

但是,对于这种特殊情况下的空对象,它可以正常工作:

select * from test where foo::text <> '{}'::text;

在9.3中,可以对每个对象中的对进行计数,并过滤没有的对

create table test (foo json);
insert into test (foo) values
('{"a":1, "c":2}'), ('{"b":1}'), ('{}');

select *
from test
where (select count(*) from json_each(foo) s) = 0;
 foo 
-----
 {}
或者测试是否存在,对于大型对象可能更快

select *
from test
where not exists (select 1 from json_each(foo) s);

从PostgreSQL 9.5开始,无论格式如何,这两种技术都能完美地工作。使用JSON数据进行这种类型的查询是不可能的。另一方面,我同意这将非常有用,并为此提出了一项请求:

请随意投票,希望它能得到实施

空JSON数组[]也可能相关

那么这对[]和{}都有效:


你必须小心。将所有数据强制转换为不同的类型,以便在大型数据库上进行比较,这将导致性能问题

如果数据具有一致的密钥,则可以查找该密钥的存在。例如,如果计划数据为{}或{id:'1'}

然后您可以查找没有“id”的项目

SELECT * FROM public."user"
where NOT(plan ? 'id')
根据需要,您可以使用双箭头函数->>获取json对象或数组字段作为文本。然后对字符串执行相等性检查

这对我来说很有效:

SELECT jsonb_col from my_table
WHERE jsonb_col ->> 'key' = '{}';
或者,如果嵌套了多个级别,请使用路径函数>>

撰写本文时当前支持的版本:

支持的版本:当前13/12/11/10/9.6


可能很明显,但这也适用于空数组只要将{}替换为[]如果要查找嵌套结构,可以使用以下内容:select*from test where foo->>'property'='[];其中的结构可能类似:{property:[],foo:bar}如果有很多行,并且每个foo都是一个大的结构,这是非常糟糕的;每个人都被强迫发短信!当我使用这个时,我不想添加“{}”::文本。还好吗?我没有使用foo::text。在这些示例中,每次调用json_后为什么都使用s?它的用途是什么?@Stratus3D它是子查询的强制别名,在本例中是一个函数。这也适用于数组,方法是将每个json_替换为json_数组_元素
SELECT jsonb_col from my_table
WHERE jsonb_col ->> 'key' = '{}';
SELECT jsonb_col from my_table
WHERE jsonb_col #>> '{key, nestedKey}' = '{}';