postgresql - Find row where an exact JSON key value pair exists - Stack Overflow

I have a table in PostgreSQL that contains two columns, one being a bigint primary key and the other co

I have a table in PostgreSQL that contains two columns, one being a bigint primary key and the other contains JSONB with a mix of arrays and simple key value pairs.

If I wanted to return the rows in the table where the JSON data in the second column contains a certain exact key value pair, what would the query need to look like? The second column is a JSONB column.

I have been reviewing the PostgreSQL documentation, but I cannot find the operators I would need to make this happen.

I have a table in PostgreSQL that contains two columns, one being a bigint primary key and the other contains JSONB with a mix of arrays and simple key value pairs.

If I wanted to return the rows in the table where the JSON data in the second column contains a certain exact key value pair, what would the query need to look like? The second column is a JSONB column.

I have been reviewing the PostgreSQL documentation, but I cannot find the operators I would need to make this happen.

Share Improve this question edited Feb 3 at 5:40 Ken White 126k15 gold badges236 silver badges466 bronze badges asked Feb 3 at 3:52 ma89581ma89581 1
Add a comment  | 

1 Answer 1

Reset to default 1

Your question is not totally clear on that, but if the key-value pair you are looking for is not at the top level of the JSON structure, your best option would be to use a JSONPATH search.

This example searches for a { "key": "value" } pair anywhere nested in a JSON structure:

SELECT JSONB '{
                "x": [
                       { "key": "value" },
                       { "key": "othervalue" }
                     ]
              }'
       @@ 'strict $.**.key == "value"';

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745250205a4618633.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信