postgresql – How to search for null values ​​in a json field

Question:

There is a table with a json column. I need to search for this field by certain fields, for example, find all lines with entity => 5 . Everything is simple here. We write a selection with the following condition field->>'entity' = '5' .

The second case is to find all lines where there is no entity key at all. Here, too, everything is simple. field->>'entity' condition field->>'entity' IS NULL

Now how can I combine these two cases so that I pass a parameter that is either 5 or null the same script? Maybe you can somehow compare it with zero? = null doesn't work ((

Answer:

select * from some_table where field->'entity' = 'null'::jsonb;
Scroll to Top