Suchen nach allen Zeilen, in denen eine bestimmte JSON-Spalte ein leeres Objekt enthält, {}
. Dies ist mit JSON-Arrays möglich, oder wenn ich nach einem bestimmten Schlüssel im Objekt suche. Aber ich möchte nur wissen, ob das Objekt leer ist. Ich kann keinen Operator finden, der das tut.Wie Abfrage einer JSON-Spalte für leere Objekte?
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.
Frisch aus dem Ofen: http://www.postgresql.org/about/news/1557/ – opyate
Wahrscheinlich offensichtlich, aber das funktioniert auch für leere Arrays ersetzen Sie einfach die {} mit [] – hobberwickey
Wenn Sie suchen verschachtelte Strukturen, könnten Sie folgendes verwenden: 'Wählen Sie * aus test where foo - >> 'property' = '[]';' wobei die Struktur etwa so aussehen könnte: '{" property ": []," foo ":" bar "}' – Dynom