Plsql 如何通过SQLPL/JSON生成JSON

Plsql 如何通过SQLPL/JSON生成JSON,plsql,pljson,Plsql,Pljson,我正在尝试从带有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'); pro

我正在尝试从带有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类型(最新版本可在中找到):如果您使用的是旧版本的库,那么将出现的任何“pljson”替换为“json”就足够了(但我建议升级,否则您可能会在Oracle 18或更高版本上遇到问题)


在这段代码中,我使用了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
                }
            ]
        }
    ]
}

@阿基米德因此,列表中的列表

{
    "customer_id": 1,
    "customer_name": "Customer A",
    "product": [
        {
            "product_id": 5715,
            "product_name": "Product A",
            "list" : [
                {
                    "a" : 1,
                    "b": 2
                },
                {
                    "a" : 10,
                    "b" : 20
                }
            ]
        }
    ]
}
我假设“列表”来自某个表格:

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.

我假设“列表”来自某个表格:

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.

如果我想在产品中添加一个json对象列表,然后在客户中添加产品,我不知道我是否理解:您能发布一个您想要的最终输出的示例吗?因此,列表中的列表如果我想在产品中添加json对象列表,然后在客户中添加产品不确定我是否理解:您可以发布您想要的最终输出的示例吗?因此,列表中的列表