Cercare di trovare tutte le righe in cui una determinata colonna json contiene un oggetto vuoto, {}
. Questo è possibile con gli array JSON, o se sto cercando una chiave specifica nell'oggetto. Ma voglio solo sapere se l'oggetto è vuoto. Non riesco a trovare un operatore che lo farà.Come interrogare una colonna JSON per oggetti vuoti?
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.
Fresh from the oven: http://www.postgresql.org/about/news/1557/ – opyate
Probabilmente ovvio ma questo funziona anche per gli array vuoti, basta sostituire il {} con [] – hobberwickey
Se stai cercando strutture nidificate, il seguente potrebbe essere qualcosa che potreste usare: 'select * from test where foo - >> 'property' = '[]';' dove la struttura potrebbe essere qualcosa del tipo: '{" proprietà ": []," foo ":" bar "}' – Dynom