如何通过SQL生成JSONPL/JSON
How to generate JSON through SQL PL/JSON
我正在尝试使用 PL/JSON
从 SQL 生成 JSON
declare
customer json_list := json_list();
product json_list;
begin
customer:= json_dyn.executeList('SELECT DISTINCT
A.customer_id,
A.customer_name,
FROM customer A
WHERE A.customer_id = 1');
product := json_dyn.executeList('SELECT DISTINCT
A.product_id,
A.product_name,
FROM sales A
INNER JOIN customer B
ON A.customer_id = B.customer_id
WHERE A.customer_id = 1');
end;
我需要的是将这两个 select 合并为一个 JSON,如下所示:
在某种程度上,产品是销售键,产品的价值是产品列表
[
{
"customer_id": 1,
"customer_name": "Customer A",
"product": [
{
"product_id": 5715,
"product_name": "Product A",
},
{
"product_id": 7841,
"product_name": "Product B",
}
]
}
]
有人知道怎么做吗?
declare
v_customers pljson_list := pljson_list();
v_customer pljson;
v_products pljson_list;
begin
for c in (select distinct customer_id, customer_name
from customer
where customer_id = 1) loop
v_customer := pljson();
v_customer.put('customer_id', c.customer_id);
v_customer.put('customer_name', c.customer_name);
v_products := json_dyn.executeList('select distinct product_id, product_name
from sales
where customer_id = ' || c.customer_id);
v_customer.put('products', v_products.to_json_value);
v_customers.append(v_customer.to_json_value);
end loop;
end;
在此代码段中,我使用了 pljson 类型(最新版本在 https://github.com/pljson/pljson 找到):如果您使用的是旧版本的库,请替换任何出现的“pljson" 和 "json" 应该足够了(但我建议升级,否则您可能在 Oracle 18 或更高版本上遇到问题)。
@archimede 因此,列表中的列表
{
"customer_id": 1,
"customer_name": "Customer A",
"product": [
{
"product_id": 5715,
"product_name": "Product A",
"list" : [
{
"a" : 1,
"b": 2
},
{
"a" : 10,
"b" : 20
}
]
}
]
}
我假设“列表”来自一些 table:
declare
v_customers pljson_list := pljson_list();
v_products pljson_list;
v_lists pljson_list;
v_customer pljson;
v_product pljson;
begin
for c in (select distinct customer_id, customer_name
from customer
where customer_id = 1) loop
v_customer := pljson();
v_customer.put('customer_id', c.customer_id);
v_customer.put('customer_name', c.customer_name);
v_products := pljson_list();
for p in (select distinct product_id, product_name
from sales
where customer_id = c.customer_id) loop
v_product := pljson();
v_product.put('product_id', p.product_id);
v_product.put('product_name', p.product_name);
v_lists := json_dyn.executeList('select distinct a, b
from lists
where product_id = ' || p.product_id);
v_product.put('list', v_lists.to_json_value);
v_products.append(v_product.to_json_value);
end loop;
v_customer.put('products', v_products.to_json_value);
v_customers.append(v_customer.to_json_value);
end loop;
end;
HTH.
我正在尝试使用 PL/JSON
从 SQL 生成 JSONdeclare
customer json_list := json_list();
product json_list;
begin
customer:= json_dyn.executeList('SELECT DISTINCT
A.customer_id,
A.customer_name,
FROM customer A
WHERE A.customer_id = 1');
product := json_dyn.executeList('SELECT DISTINCT
A.product_id,
A.product_name,
FROM sales A
INNER JOIN customer B
ON A.customer_id = B.customer_id
WHERE A.customer_id = 1');
end;
我需要的是将这两个 select 合并为一个 JSON,如下所示: 在某种程度上,产品是销售键,产品的价值是产品列表
[
{
"customer_id": 1,
"customer_name": "Customer A",
"product": [
{
"product_id": 5715,
"product_name": "Product A",
},
{
"product_id": 7841,
"product_name": "Product B",
}
]
}
]
有人知道怎么做吗?
declare
v_customers pljson_list := pljson_list();
v_customer pljson;
v_products pljson_list;
begin
for c in (select distinct customer_id, customer_name
from customer
where customer_id = 1) loop
v_customer := pljson();
v_customer.put('customer_id', c.customer_id);
v_customer.put('customer_name', c.customer_name);
v_products := json_dyn.executeList('select distinct product_id, product_name
from sales
where customer_id = ' || c.customer_id);
v_customer.put('products', v_products.to_json_value);
v_customers.append(v_customer.to_json_value);
end loop;
end;
在此代码段中,我使用了 pljson 类型(最新版本在 https://github.com/pljson/pljson 找到):如果您使用的是旧版本的库,请替换任何出现的“pljson" 和 "json" 应该足够了(但我建议升级,否则您可能在 Oracle 18 或更高版本上遇到问题)。
@archimede 因此,列表中的列表
{
"customer_id": 1,
"customer_name": "Customer A",
"product": [
{
"product_id": 5715,
"product_name": "Product A",
"list" : [
{
"a" : 1,
"b": 2
},
{
"a" : 10,
"b" : 20
}
]
}
]
}
我假设“列表”来自一些 table:
declare
v_customers pljson_list := pljson_list();
v_products pljson_list;
v_lists pljson_list;
v_customer pljson;
v_product pljson;
begin
for c in (select distinct customer_id, customer_name
from customer
where customer_id = 1) loop
v_customer := pljson();
v_customer.put('customer_id', c.customer_id);
v_customer.put('customer_name', c.customer_name);
v_products := pljson_list();
for p in (select distinct product_id, product_name
from sales
where customer_id = c.customer_id) loop
v_product := pljson();
v_product.put('product_id', p.product_id);
v_product.put('product_name', p.product_name);
v_lists := json_dyn.executeList('select distinct a, b
from lists
where product_id = ' || p.product_id);
v_product.put('list', v_lists.to_json_value);
v_products.append(v_product.to_json_value);
end loop;
v_customer.put('products', v_products.to_json_value);
v_customers.append(v_customer.to_json_value);
end loop;
end;
HTH.