I have table users
with json column details
.
I want to fetch all user records where details[\"email\"] is null or email key doesn\'t exist.
You need to use the ->>
operator for that:
select *
from users
where (details->>'email') is not null
Because the ->
operator returns 'null'::json
(and not sql NULL
) if the key is exists but with a json null value.
http://sqlfiddle.com/#!15/76ec4/2
use brackets ()
. Looks like compiler tries to see it like details->('email' IS NOT NULL)
. So you can fix it like this:
select *
from users
where (details->'email') is not null
sql fiddle demo
actually, to get records where details["email"] is null or email key doesn't exist, you can use this query:
select *
from users
where (details->>'email') is null
as described in this answer.