[英]Postgres - query json with nested arrray and objects inside array
我將數據作為 jsonb 存儲在 Postgres 12 表中,jsonb 的結構在數組內部有一個數組。
如何從嵌套數組中獲取值? 我可以從一級數組中獲取值,但不能從二級數組中獲取值。
這是 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"
}
}
]
}
使用帶有 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)
這是迄今為止的結果
| id | external_order_id | external_customer_id | sku |
|----|-------------------|----------------------|-------|
| 1 | 2 | 3 | SKU-1 |
| 1 | 2 | 3 | SKU-2 |
我想要實現的是。 理想情況下,這將在不知道屬性名稱的值的情況下實現
| 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 |
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.