Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if field exists in json type column PostgreSQL

How do you check if a json field in Postgres has a certain element?

I have tried with json->>'attribute' is not null and doesn't work.

like image 558
Alexandru R Avatar asked Oct 17 '13 08:10

Alexandru R


2 Answers

use ->:

where (json->'attribute') is not null 
like image 163
Roman Pekar Avatar answered Oct 22 '22 17:10

Roman Pekar


While this works. It is better to use special operator ?:

WHERE your_column_name::jsonb ? 'attribute' 

NOTE: Only for jsonb type.

like image 25
Eugen Konkov Avatar answered Oct 22 '22 18:10

Eugen Konkov