20

Consider this example:

postgres=# CREATE TABLE emptyarray (fields jsonb);                                                                                                                            
CREATE TABLE                                                                                                                                                                  
postgres=# INSERT INTO emptyarray VALUES ('{"key":["a","b"]}');                                                                                                               
INSERT 0 1                                                                                                                                                                    
postgres=# INSERT INTO emptyarray VALUES ('{"key":[]}');                                                                                                                      
INSERT 0 1                                                                                                                                                                    
postgres=# SELECT * from emptyarray where Fields@>'{"key":["b"]}';                                                                                                            
       fields                                                                                                                                                                 
---------------------                                                                                                                                                         
 {"key": ["a", "b"]}                                                                                                                                                          
(1 row)                                                                                                                                                                       

postgres=# SELECT * from emptyarray where Fields@>'{"key":[]}';                                                                                                               
       fields                                                                                                                                                                 
---------------------                                                                                                                                                         
 {"key": ["a", "b"]}                                                                                                                                                          
 {"key": []}                                                                                                                                                                  
(2 rows)

In the second query I expected only one rows in the results (the one record with empty array). But as you can see there are two rows in the result. How do I query for a empty array using @> syntax?

I am using PostgreSQL 9.6

1 Answer 1

27

You could use:

SELECT * from emptyarray where Fields-> 'key' = '[]'::jsonb;

Rextester Demo

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! So, the @> syntax is not going to work for empty array?
@baijum Well I could not say 100% sure. I just propose alternative.
I was generating the queries, so a consistent syntax would have been easier :(
@LukaszSzozda - What if there is no key? I have a jsonb which is just an empty array like this [].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.