Szukając wszystkich wierszy, w których pewna kolumna json zawiera pusty obiekt, {}
. Jest to możliwe z tablicami JSON lub gdy szukam określonego klucza w obiekcie. Ale chcę tylko wiedzieć, czy obiekt jest pusty. Nie można znaleźć operatora, który to zrobi.Jak wykonać zapytanie o kolumnę json dla pustych obiektów?
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.
Świeżo z pieca: http://www.postgresql.org/about/news/1557/ – opyate
Prawdopodobnie jest to oczywiste, ale działa to również w przypadku pustych tablic, wystarczy wymienić {} na [] – hobberwickey
Jeśli szukasz zagnieżdżone struktury, może być coś, co możesz użyć: 'wybierz * z testu gdzie foo - >> 'właściwość' = '[]';' gdzie struktura może wyglądać następująco: '{" własność ": []," foo ":" bar "}' – Dynom