如何从 Postgresql 中的 JSON 对象中获取值并插入到其他表中?
How to grab the value from JSON object in Postgresql and insert into some other tables?
{
"orderId": "s",
"fulfillerId": "qaqj8fkbmb",
"orderDetailsUrl": "asd",
"items": [
{
"dTe": "laseIn",
"itemDescription": "Tetesting",
"itemId": "item_1",
"manufacturingUrl": "https://uploads_1.documents.press",
"skuCode": "1223",
"productName": "Tesing",
"quantity": 225,
"taskId": "task_ID_1"
},
{
"dTe": "laseIn",
"itemDescription": "Test Sku for Oracle testing",
"itemId": "item_2",
"manufacturingUrl": "https://uploads_2.documents.press",
"skuCode": "123",
"productName": "Test Sku for Oracle testing",
"quantity": 225,
"taskId": "task_ID_2"
}
]
}
实施
-> 获取 orderId/fulfillerId/orderDetailsUrl 的值并对项目数组对象执行迭代以获取所需的值作为其相似对象的数组
-> 存储在变量中
->然后执行插入操作
不知何故它不起作用
期望输出:
项目Table
itemId
fullfillerId
订单号
skuCode
++ 项目 Table
中 JSON 的所有剩余数据
物品详情Table
itemId
orderDetailsUrl
taskId
数量
我尝试了各种 jsonb 格式,但解析后无法获得准确的输出,我无法从 JSON 中选取值并使用 Insert 查询
这里有一个 plpgsql 块,用于说明如何执行此操作。 raise notice
将替换为相关命令,例如 insert
。
do language plpgsql
$$
declare
j constant json default $json$
{
"orderId": "s",
"fulfillerId": "qaqj8fkbmb",
"orderDetailsUrl": "asd",
"items": [
{
"dTe": "laseIn",
"itemDescription": "Tetesting",
"itemId": "item_1",
"manufacturingUrl": "https://uploads_1.documents.press",
"skuCode": "1223",
"productName": "Tesing",
"quantity": 225,
"taskId": "task_ID_1"
},
{
"dTe": "laseIn",
"itemDescription": "Test Sku for Oracle testing",
"itemId": "item_2",
"manufacturingUrl": "https://uploads_2.documents.press",
"skuCode": "123",
"productName": "Test Sku for Oracle testing",
"quantity": 225,
"taskId": "task_ID_2"
}
]
}
$json$;
r record;
begin
-- Item
for r in select j->>'fulfillerId' fullfillerid, j->>'orderId' orderid, t->>'itemId' itemid, t->>'skuCode' skucode
from json_array_elements(j -> 'items') t loop
raise notice '% % % %', r.fullfillerid, r.orderid, r.itemid, r.skucode;
end loop;
--ItemDetails
for r in select j->>'orderDetailsUrl' orderdetailsurl, t->>'itemId' itemid, t->>'taskId' taskid, (t->>'quantity')::numeric quantity
from json_array_elements(j -> 'items') t loop
raise notice '% % % %', r.orderdetailsurl, r.itemid, r.taskid, r.quantity;
end loop;
end;
$$;
一个插入会更简单,w/o一个循环:
insert into target_table ( list - of - columns )
select
j->>'orderDetailsUrl' orderdetailsurl,
t->>'itemId' itemid,
t->>'taskId' taskid,
(t->>'quantity')::numeric quantity
-- other expressions maybe
from json_array_elements(j -> 'items') t;
作为插入 item
table 和 itemietails
的函数 table:
create or replace function json_items(raw_data json)
returns void language plpgsql as
$$
begin
insert into itemdetails(itemid, orderdetailsurl, taskid, quantity)
select
t->>'itemId',
raw_data->>'orderDetailsUrl',
t->>'taskId',
(t->>'quantity')::numeric
from json_array_elements(raw_data -> 'items') t;
insert into item(itemid, fullfillerid, orderid, skucode, dte, itemdescription)
select
t->>'itemId',
raw_data->>'fullfillerId',
raw_data->>'orderId',
t->>'skuCode',
t->>'dte',
t->>'itemDescription'
from json_array_elements(raw_data -> 'items') t;
end;
$$;
{
"orderId": "s",
"fulfillerId": "qaqj8fkbmb",
"orderDetailsUrl": "asd",
"items": [
{
"dTe": "laseIn",
"itemDescription": "Tetesting",
"itemId": "item_1",
"manufacturingUrl": "https://uploads_1.documents.press",
"skuCode": "1223",
"productName": "Tesing",
"quantity": 225,
"taskId": "task_ID_1"
},
{
"dTe": "laseIn",
"itemDescription": "Test Sku for Oracle testing",
"itemId": "item_2",
"manufacturingUrl": "https://uploads_2.documents.press",
"skuCode": "123",
"productName": "Test Sku for Oracle testing",
"quantity": 225,
"taskId": "task_ID_2"
}
]
}
实施
-> 获取 orderId/fulfillerId/orderDetailsUrl 的值并对项目数组对象执行迭代以获取所需的值作为其相似对象的数组
-> 存储在变量中
->然后执行插入操作
不知何故它不起作用
期望输出:
项目Table
itemId
fullfillerId
订单号
skuCode
++ 项目 Table
中 JSON 的所有剩余数据物品详情Table
itemId
orderDetailsUrl
taskId
数量
我尝试了各种 jsonb 格式,但解析后无法获得准确的输出,我无法从 JSON 中选取值并使用 Insert 查询
这里有一个 plpgsql 块,用于说明如何执行此操作。 raise notice
将替换为相关命令,例如 insert
。
do language plpgsql
$$
declare
j constant json default $json$
{
"orderId": "s",
"fulfillerId": "qaqj8fkbmb",
"orderDetailsUrl": "asd",
"items": [
{
"dTe": "laseIn",
"itemDescription": "Tetesting",
"itemId": "item_1",
"manufacturingUrl": "https://uploads_1.documents.press",
"skuCode": "1223",
"productName": "Tesing",
"quantity": 225,
"taskId": "task_ID_1"
},
{
"dTe": "laseIn",
"itemDescription": "Test Sku for Oracle testing",
"itemId": "item_2",
"manufacturingUrl": "https://uploads_2.documents.press",
"skuCode": "123",
"productName": "Test Sku for Oracle testing",
"quantity": 225,
"taskId": "task_ID_2"
}
]
}
$json$;
r record;
begin
-- Item
for r in select j->>'fulfillerId' fullfillerid, j->>'orderId' orderid, t->>'itemId' itemid, t->>'skuCode' skucode
from json_array_elements(j -> 'items') t loop
raise notice '% % % %', r.fullfillerid, r.orderid, r.itemid, r.skucode;
end loop;
--ItemDetails
for r in select j->>'orderDetailsUrl' orderdetailsurl, t->>'itemId' itemid, t->>'taskId' taskid, (t->>'quantity')::numeric quantity
from json_array_elements(j -> 'items') t loop
raise notice '% % % %', r.orderdetailsurl, r.itemid, r.taskid, r.quantity;
end loop;
end;
$$;
一个插入会更简单,w/o一个循环:
insert into target_table ( list - of - columns )
select
j->>'orderDetailsUrl' orderdetailsurl,
t->>'itemId' itemid,
t->>'taskId' taskid,
(t->>'quantity')::numeric quantity
-- other expressions maybe
from json_array_elements(j -> 'items') t;
作为插入 item
table 和 itemietails
的函数 table:
create or replace function json_items(raw_data json)
returns void language plpgsql as
$$
begin
insert into itemdetails(itemid, orderdetailsurl, taskid, quantity)
select
t->>'itemId',
raw_data->>'orderDetailsUrl',
t->>'taskId',
(t->>'quantity')::numeric
from json_array_elements(raw_data -> 'items') t;
insert into item(itemid, fullfillerid, orderid, skucode, dte, itemdescription)
select
t->>'itemId',
raw_data->>'fullfillerId',
raw_data->>'orderId',
t->>'skuCode',
t->>'dte',
t->>'itemDescription'
from json_array_elements(raw_data -> 'items') t;
end;
$$;