Check if field exists in json type column PostgreSQL

JsonPostgresql

Json Problem Overview


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.

Json Solutions


Solution 1 - Json

use ->:

where (json->'attribute') is not null

Solution 2 - Json

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

WHERE your_column_name::jsonb ? 'attribute'

NOTE: Only for jsonb type.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexandru RView Question on Stackoverflow
Solution 1 - JsonRoman PekarView Answer on Stackoverflow
Solution 2 - JsonEugen KonkovView Answer on Stackoverflow