简体   繁体   English

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

[英]Postgres - query json with nested arrray and objects inside array

I have data stored in a Postgres 12 table as jsonb, the structure of the jsonb has an array inside an array.我将数据作为 jsonb 存储在 Postgres 12 表中,jsonb 的结构在数组内部有一个数组。

How do i get the values from the nested array?如何从嵌套数组中获取值? I can get the values from the first level array but not the second level array.我可以从一级数组中获取值,但不能从二级数组中获取值。

This is a simplified example of the json这是 json 的简化示例

{
    "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"
            }
        }
    ]
}

using jsonb_to_record and jsonb_to_recordset with LATERAL and CROSS JOIN LATERAL, i am able to get the values from the nodes and the first level array使用带有 LATERAL 和 CROSS JOIN LATERAL 的 jsonb_to_record 和 jsonb_to_recordset,我能够从节点和第一级数组中获取值

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)

This is the result so far这是迄今为止的结果

| id | external_order_id | external_customer_id | sku   |
|----|-------------------|----------------------|-------|
| 1  | 2                 | 3                    | SKU-1 |
| 1  | 2                 | 3                    | SKU-2 |

what i want to achieve is.我想要实现的是。 ideally this would be achieved without knowing the values of the property names理想情况下,这将在不知道属性名称的值的情况下实现

| 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          |

dbfiddle 小提琴手

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM