PostgreSQL JSON – How to Fetch Exact Row Using JSON Column

jsonpostgresql

I have records in table like below format.

Table struture

CREATE TABLE test.orders (
id serial NOT NULL,
info json NOT NULL,
value varchar NULL,
CONSTRAINT orders_pkey PRIMARY KEY (id)

);

enter image description here

If I pass value for info column {"101":1} then only value s1 should be fetched not the others.

So is there any feasible way in postges to fetch exact this type of result?

Thanks

Best Answer

If you want those where the value matches exactly the key/value pair, use = with a jsonb type.

select *
from the_table
where info::jsonb = '{"101": 1}'

As your column is not defined as jsonb (which it should be) you have to cast it before you can compare it.