Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select postgres rows where at least one json element matches some criteria?

Here's what I'm working with:

create table test(id INT, data JSON);
INSERT into test values
  (1, '[{"key": 2}, {"key": 1}]'),
  (2, '[{"key": 3}]'),
  (3, '[{"key": 1}]');


select * from test;
select id from test where 1 == ANY( json_array_elements(data) ->> 'key');

What I'm trying to do is select all rows where any of the json objects in the data column have a key key with a value of 1. I trying to extract rows 1 and 3. Note, I'm not sure if the equality comparison == right before the ANY clause is correct.

When I run the above, I get the following error: ERROR: set-returning functions are not allowed in WHERE

like image 807
Paymahn Moghadasian Avatar asked Dec 17 '25 20:12

Paymahn Moghadasian


1 Answers

If you are free to use jsonb instead of json (which is preferable in most cases), use the jsonb "contains" operator @>:

SELECT *
FROM   test
WHERE  data  @> '[{"key": 1"}]';

Can be supported with a GIN index with default operator class or with the more specialized jsonb_path_ops:

CREATE INDEX test_data_gin_idx ON test USING gin (data jsonb_path_ops);

db<>fiddle here

Related:

  • Index for finding an element in a JSON array
like image 180
Erwin Brandstetter Avatar answered Dec 19 '25 12:12

Erwin Brandstetter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!