想要查找包含空对象{}
的特定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.
select * from test where foo->>'property' = '[]';
其中的结构可能类似于:{ "property": [], "foo": "bar" }
。请注意,该语句仅返回具有名为“property”的空数组属性的行。 - Dynomfoo
都是一个大结构体;每个结构体都被强制转换为文本! - EoghanM