[英]How to filter a value of nested key of json in postgres
I have a table in PostgreSQL我在 PostgreSQL 有一张桌子
CREATE TABLE IF NOT EXISTS account_details
(
account_id integer,
condition json
)
And the data's present inside this table are这张表中的数据是
account_id![]() |
condition![]() |
---|---|
1 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[35,20,5,6]}}}] ![]() |
2 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[1,4,2,3]}}}] ![]() |
3 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[5]}}}] ![]() |
4 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[1,2,3,5]}}}] ![]() |
5 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[3,4]}}}] ![]() |
6 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[4]}}}] ![]() |
7 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[3]}}}] ![]() |
I need to fetch part of values present inside rootcompanyid我需要获取rootcompanyid中存在的部分值
Please guide me how to filter nested JSON data请指导我如何过滤嵌套的 JSON 数据
Expected Output预计 Output
Case: 1情况1
Whenever I pass input rootcompanyid = 5 to the below query, it should filter out account_id 1,3 and 4 details每当我将输入 rootcompanyid = 5 传递给下面的查询时,它应该过滤掉 account_id 1,3 和 4 的详细信息
SELECT *
FROM account_details
WHERE ((condition->0->>'conditions')::json->>'rootcompanyid')::json->>'$in' = 5;
account_id![]() |
condition![]() |
---|---|
1 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[35,20, 5 ,6]}}}] ![]() |
3 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[ 5 ]}}}] ![]() |
4 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[1,2,3, 5 ]}}}] ![]() |
Case: 2案例:2
Whenever I pass input rootcompanyid = 3,4 to the below query, it should filter out account_id 2,4,5,6 and 7 details每当我将输入 rootcompanyid = 3,4 传递给下面的查询时,它应该过滤掉 account_id 2,4,5,6 和 7 的详细信息
SELECT *
FROM account_details
WHERE ((condition->0->>'conditions')::json->>'rootcompanyid')::json->>'$in' = 3,4;
account_id![]() |
condition![]() |
---|---|
2 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[1, 4 ,2, 3 ]}}}] ![]() |
4 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[1,2, 3 ,5]}}}] ![]() |
5 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[ 3 , 4 ]}}}] ![]() |
6 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[ 4 ]}}}] ![]() |
7 ![]() |
[{"action":"read","subject":"rootcompany","conditions":{"rootcompanyid":{"$in":[ 3 ]}}}] ![]() |
Please give me the best solution to filter partial and matching rootcompanyid in JSON format请给我最好的解决方案来过滤部分和匹配的JSON格式的 rootcompanyid
You need to compare the element in the nested json
.您需要比较嵌套的
json
中的元素。 Here is an example:这是一个例子:
SELECT *
FROM account_details
WHERE ((condition->0->>'conditions')::json->>'rootcompanyid')::json->>'$in' = '[35,20,5,6]';
condition->0
- first element of array condition->0
- 数组的第一个元素
->>'conditions')::json
- extract conditions
from first element of array ->>'conditions')::json
- 从数组的第一个元素中提取conditions
->>'rootcompanyid')::json
- extract rootcompanyid
from conditions
->>'rootcompanyid')::json
- 从conditions
中提取rootcompanyid
::json->>'$in'
- extract $in
from rootcompanyid
(id's of rootcompanyid
) ::json->>'$in'
- 从rootcompanyid
( rootcompanyid
的 id)中提取$in
Search by action:按动作搜索:
SELECT *
FROM account_details
WHERE condition->0->>'action' = 'read';
Updated更新
To search specific id
in json array
you can use @>
operator:要在
json array
中搜索特定id
,您可以使用@>
运算符:
SELECT *
FROM account_details
WHERE ((((condition->0->>'conditions')::json->>'rootcompanyid')::json->>'$in')::jsonb) @> '[3,1]'::jsonb
Or LIKE
:或者
LIKE
:
SELECT *
FROM account_details
WHERE ((condition->0->>'conditions')::json->>'rootcompanyid')::json->>'$in' LIKE '%5%';
Updated v2更新 v2
You can use json_array_elements_text
+ exists
:您可以使用
json_array_elements_text
+ exists
:
WITH parsed_conditions AS (
SELECT account_id,
json_array_elements_text(
(((condition -> 0 ->> 'conditions')::json ->> 'rootcompanyid')::json ->>'$in')::json
) AS json_id
FROM account_details
)
SELECT *
FROM account_details AS a
WHERE exists(
SELECT DISTINCT account_id
FROM parsed_conditions
WHERE json_id IN ('3', '1')
AND parsed_condidtions.account_id = a.account_id
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.