首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Postgres -使用嵌套数组和数组中的对象查询json

Postgres -使用嵌套数组和数组中的对象查询json
EN

Stack Overflow用户
提问于 2021-12-10 20:01:59
回答 1查看 720关注 0票数 2

我将数据存储在Postgres 12表中,作为jsonb,jsonb的结构在数组中有一个数组。

如何从嵌套数组中获取值?我可以从第一级数组获得值,但不能从第二级数组中获取值。

这是json的一个简化示例。

代码语言:javascript
复制
{
    "id": 1,
    "external_order_id": {
        "id": "2"
    },
    "customer": {
        "external_customer_id": {
            "id": "3"
        }
    },
    "line_items": [
        {
            "sku": "SKU-1",
            "properties": [
                {
                    "name": "colour",
                    "value": "red"
                },
                {
                    "name": "size",
                    "value": "large"
                }
            ],
            "external_product_id": {
                "id": "4"
            },
            "external_variant_id": {
                "id": "5"
            }
        },
        {
            "sku": "SKU-2",
            "properties": [
                {
                    "name": "colour",
                    "value": "black"
                },
                {
                    "name": "size",
                    "value": "small"
                }
            ],
            "external_product_id": {
                "id": "8"
            },
            "external_variant_id": {
                "id": "9"
            }
        }
    ]
}

使用具有横向和交叉连接侧的jsonb_to_record和jsonb_to_recordset,可以从节点和第一级数组中获得值。

代码语言:javascript
复制
WITH data(content) AS ( VALUES
  ('{
    "id": 1,
         "external_order_id": {
        "id": "2"
    },
    "customer": {
        
        "external_customer_id": {
            "id": "3"
        }
    },
    "line_items": [
        {
            "sku": "SKU-1",
            "properties": [
                {
                    "name": "colour",
                    "value": "red"
                },
                {
                    "name": "size",
                    "value": "large"
                }
            ],
            "external_product_id": {
                "id": "4"
            },
            "external_variant_id": {
                "id": "5"
            }
        },
                {
            "sku": "SKU-2",
            "properties": [
                {
                    "name": "colour",
                    "value": "black"
                },
                {
                    "name": "size",
                    "value": "small"
                }
            ],
            "external_product_id": {
                "id": "8"
            },
            "external_variant_id": {
                "id": "9"
            }
        }
    ]
   
}'::jsonb)
)
select ord.*
,ext.id as external_order_id
,cus.id as external_customer_id
,line_items.*

FROM data,
jsonb_to_record(content) as ord(id int),
LATERAL jsonb_to_record(content->'external_order_id') as ext(id text),
LATERAL jsonb_to_record(content#>'{customer, external_customer_id}') as cus(id text)
CROSS JOIN LATERAL jsonb_to_recordset(content->'line_items') line_items(sku text)

这是目前为止的结果。

代码语言:javascript
复制
| id | external_order_id | external_customer_id | sku   |
|----|-------------------|----------------------|-------|
| 1  | 2                 | 3                    | SKU-1 |
| 1  | 2                 | 3                    | SKU-2 |

我想要实现的是。理想情况下,这是在不知道属性名称的值的情况下实现的。

代码语言:javascript
复制
| id | external_order_id | external_customer_id | sku   | external_product_id | external_variant_id | property_name | property_value |
|----|-------------------|----------------------|-------|---------------------|---------------------|---------------|----------------|
| 1  | 2                 | 3                    | SKU-1 | 4                   | 5                   | colour        | red            |
| 1  | 2                 | 3                    | SKU-1 | 4                   | 5                   | size          | large          |
| 1  | 2                 | 3                    | SKU-2 | 8                   | 9                   | colour        | black          |
| 1  | 2                 | 3                    | SKU-2 | 8                   | 9                   | size          | small          |

[医]小提琴

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-10 21:06:28

代码语言:javascript
复制
WITH data(content) AS ( VALUES
  ('{
    "id": 1,
         "external_order_id": {
        "id": "2"
    },
    "customer": {

        "external_customer_id": {
            "id": "3"
        }
    },
    "line_items": [
        {
            "sku": "SKU-1",
            "properties": [
                {
                    "name": "colour",
                    "value": "red"
                },
                {
                    "name": "size",
                    "value": "large"
                }
            ],
            "external_product_id": {
                "id": "4"
            },
            "external_variant_id": {
                "id": "5"
            }
        },
                {
            "sku": "SKU-2",
            "properties": [
                {
                    "name": "colour",
                    "value": "black"
                },
                {
                    "name": "size",
                    "value": "small"
                }
            ],
            "external_product_id": {
                "id": "8"
            },
            "external_variant_id": {
                "id": "9"
            }
        }
    ]

}'::jsonb)
)
select ord.*
,ext.id as external_order_id
,cus.id as external_customer_id
,line_items.sku
,line_items.external_product_id->>'id' as external_product_id
,line_items.external_variant_id->>'id' as external_variant_id
,props.*
FROM data,
jsonb_to_record(content) as ord(id int),
LATERAL jsonb_to_record(content->'external_order_id') as ext(id text),
LATERAL jsonb_to_record(content#>'{customer, external_customer_id}') as cus(id text)
CROSS JOIN LATERAL jsonb_to_recordset(content->'line_items') line_items(sku text, properties jsonb, external_product_id jsonb, external_variant_id jsonb)
cross join LATERAL jsonb_to_recordset(line_items.properties) props(name text, value text)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70309816

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档