【问题标题】:Generate Oracle JSON from CLOB datatype column从 CLOB 数据类型列生成 Oracle JSON
【发布时间】:2020-12-06 16:50:23
【问题描述】:

要求是从 clob 数据类型列生成 JSON。 环境版本Oracle 12.2

我有一个表,其中包含字段 id(数字数据类型)和详细信息(clob 类型),如下所示

ID   - details 

100  - 134332:10.0, 1481422:1.976, 1483734:1.688, 2835036:1.371

101  - 134331:0.742, 319892:0.734, 1558987:0.7, 2132090:0.697

例如输出:

{
   "pId":100,
   "cid":[
      {
         "cId":134332,
         "wt":"10.0"
      },
      {
         "cId":1481422,
         "wt":"1.976"
      },
      {
         "cId":1483734,
         "wt":"1.688"
      },
      {
         "cId":2835036,
         "wt":"1.371"
      }
   ]
}

请帮助使用 oracle SQL 查询以生成输出。

【问题讨论】:

  • 我不明白。 pId 1788916 来自哪里?您是否将其作为 Id 最初(在输入表中),然后您将其更改为 100 但忘记在所需的输出中更改它?或者还有什么?
  • 另外:在您的输入中,details 列中的字符串是否已经包含您显示的双引号?或者这只是你表明它们是字符串的方式? (我问是因为在 Oracle 中,字符串用单引号括起来,而不是用双引号括起来。)
  • @mathguy 是的,我的错,那是错字。 pid 为 100。详细信息字段上没有双引号(尽管它是 clob)。
  • 纠正了我的问题,谢谢!

标签: json oracle oracle12c clob


【解决方案1】:

下面我设置了一个包含几个输入行的表用于测试;然后我将展示一种解决问题的方法以及该查询的输出。我没有尝试编写最有效(最快)的查询;相反,我希望这将向您展示如何做到这一点。然后,如果速度是一个问题,你可以解决这个问题。 (在这种情况下,最好先重新考虑输入,这会破坏第一范式。)

我添加了几个输入行进行测试,以了解如何处理 null。您可以决定这是否是所需的处理方式。 (您的数据中可能没有null - 在这种情况下,您应该在提问时这么说。)

设置测试表:

create table input_tbl (id number primary key, details clob);
insert into input_tbl (id, details) values
  (100, to_clob('134332:10.0, 1481422:1.976, 1483734:1.688, 2835036:1.371'));
insert into input_tbl (id, details) values
  (101, '134331:0.742, 319892:0.734, 1558987:0.7, 2132090:0.697');
insert into input_tbl (id, details) values
  (102, null);
insert into input_tbl (id, details) values
  (103, '2332042:  ');
commit;

查询:

with
  tokenized (pid, ord, cid, wt) as (
    select i.id, q.ord, q.cid, q.wt
    from   input_tbl i cross apply
           (
             select level as ord, 
                    regexp_substr(details, '(, |^)([^:]+):', 1, level, null, 2) 
                      as cid,
                    regexp_substr(details, ':([^,]*)', 1, level, null, 1) as wt
             from   dual
             connect by level <= regexp_count(details, ':')
           ) q
  )
, arrayed (pid, json_arr) as (
    select pid, json_arrayagg(json_object(key 'cId' value to_number(trim(cid)),
                                          key 'wt'  value to_number(trim(wt)))
                             )
    from   tokenized
    group  by pid
  )
select pid, json_object(key 'pId' value pid, key 'cid' value json_arr) as json
from   arrayed
;

输出:

 PID JSON                                                                                                                         
---- -----------------------------------------------------------------------------------------------------------------------------
 100 {"pId":100,"cid":[{"cId":134332,"wt":10},{"cId":2835036,"wt":1.371},{"cId":1483734,"wt":1.688},{"cId":1481422,"wt":1.976}]}  
 101 {"pId":101,"cid":[{"cId":134331,"wt":0.742},{"cId":2132090,"wt":0.697},{"cId":1558987,"wt":0.7},{"cId":319892,"wt":0.734}]}  
 102 {"pId":102,"cid":[{"cId":null,"wt":null}]}                                                                                   
 103 {"pId":103,"cid":[{"cId":2332042,"wt":null}]} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多