Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query a json column for empty objects?

Looking to find all rows where a certain json column contains an empty object, {}. This is possible with JSON arrays, or if I am looking for a specific key in the object. But I just want to know if the object is empty. Can't seem to find an operator that will do this.

 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. 
like image 296
sbeam Avatar asked Jun 18 '14 18:06

sbeam


People also ask

How do I check if a JSON object is empty?

return Object.keys(obj).length === 0 ; This is typically the easiest way to determine if an object is empty.

Can you query a JSON object?

To query JSON data, you can use standard T-SQL. If you must create a query or report on JSON data, you can easily convert JSON data to rows and columns by calling the OPENJSON rowset function. For more information, see Convert JSON Data to Rows and Columns with OPENJSON (SQL Server).

Can you have an empty array in JSON?

JSON data has the concept of null and empty arrays and objects.


2 Answers

There is no equality (or inequality) operator for the data type json as a whole, because equality is hard to establish. Consider jsonb in Postgres 9.4 or later, where this is possible. More details in this related answer on dba.SE (last chapter):

  • How to remove known elements from a JSON[] array in PostgreSQL?

SELECT DISTINCT json_column ... or ... GROUP BY json_column fail for the same reason (no equality operator).

Casting both sides of the expression to text allows = or <> operators, but that's not normally reliable as there are many possible text representations for the same JSON value. In Postgres 9.4 or later, cast to jsonb instead. (Or use jsonb to begin with.)

However, for this particular case (empty object) it works just fine:

select * from test where foo::text <> '{}'::text; 
like image 167
Erwin Brandstetter Avatar answered Oct 06 '22 05:10

Erwin Brandstetter


Empty JSON array [] could also be relevant.

Then this could work for both [] and {}:

select * from test where length(foo::text) > 2 ; 
like image 29
weinerk Avatar answered Oct 06 '22 05:10

weinerk