【问题标题】:How to generate JSON through SQL PL/JSON如何通过 SQL PL/JSON 生成 JSON
【发布时间】:2020-08-18 18:08:32
【问题描述】:

我正在尝试使用 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;

我需要的是加入这两个选择成为一个单一的 JSON,看起来像这样: 在某种程度上,产品是销售关键,产品的价值是产品列表

[
  {
    "customer_id": 1,
    "customer_name": "Customer A",
    "product": [
      {
        "product_id": 5715,
        "product_name": "Product A",
      },
      {
        "product_id": 7841,
        "product_name": "Product B",
      }
    ]
  }
]

有人知道怎么做吗?

【问题讨论】:

    标签: plsql pljson


    【解决方案1】:

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

    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。

    【讨论】:

      【解决方案2】:
      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;
      

      在这个 sn-p 中,我使用了 pljson 类型(在https://github.com/pljson/pljson 找到的最新版本):如果您使用的是旧版本的库,用“json”替换任何出现的“pljson”就足够了(但是我建议升级,否则您可能会在 Oracle 18 或更高版本上遇到问题。

      【讨论】:

      • 如果我想在产品中添加 json 对象列表,然后在客户中添加产品
      • 不确定我是否理解:你能发布一个你想要的最终输出的例子吗?
      • 因此,列表中的列表
      【解决方案3】:

      @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
                      }
                  ]
              }
          ]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-09
        • 2020-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-14
        相关资源
        最近更新 更多