The following is a sample query that you can use to search for rows that have the nth element in a JSONB array that match a specific value.
Given
CREATE TABLE sample_jsonb (
id serial PRIMARY KEY,
name varchar(64),
json_data jsonb
);
INSERT INTO sample_jsonb(name, json_data)
VALUES
('foo',
'{
"key": "val1",
"arr": ["homer", "bart", "barney"]
}'),
('bar',
'{
"key": "val2",
"arr": ["marge", "lisa", "maggie"]
}'),
('baz',
'{
"key": "val2",
"arr": ["bart", "milhouse", "nelson"]
}')
;
We now have two records → Continue reading “Query for Finding the nth Element in a PostgreSQL JSONB Array that Matches a Specified Value”