ORACLE - JSON To Key Value Pair Table

假如想象 提交于 2019-12-11 05:52:18

问题


Is there any way to obtain a table with key/value pairs from a CLOB Json Column?

The idea here is to get these values, on a dynamic way. Because the CLOB column does not always contain the same structure.

I've created a function that does this, however since it literally parses the json string, when we use it in a table with many records its very slow. And by very slow I mean like 2-5 records per second, i know it's terrible.

The Oracle tools (v.12c) do not provide a dynamic way to obtain the json tags/values, we have always to specify the paths.

I've been digging all around without any luck. Any thoughts?


回答1:


12.2 contains a set of PL/SQL objects that can be used to build a DOM like structure of a JSON document. You can then extract key lists etc using methods on the objects. Look in the 12.2 doc for JSON_OBJECT_T, JSON_ARRAY_T etc which can be used like this..

SQL> create or replace type NV_PAIR_T as object (
  2    NAME  VARCHAR2(32),
  3    VALUE VARCHAR2(32)
  4  )
  5  /

Type created.

SQL> create or replace type NV_PAIR_TABLE as TABLE of NV_PAIR_T
  2  /

Type created.

SQL> create or replace function GET_KEY_VALUES(P_JSON_DOC VARCHAR2)
  2  return NV_PAIR_TABLE PIPELINED
  3  as
  4    JO JSON_OBJECT_T := JSON_OBJECT_T(P_JSON_DOC);
  5    JO_KEYS JSON_KEY_LIST := JO.get_keys();
  6  begin
  7
  8    for i in 1..JO_KEYS.count loop
  9      pipe row (NV_PAIR_T(JO_KEYS(i),JO.get_string(JO_KEYS(i))));
 10    end loop;
 11  end;
 12  /

Function created.

SQL> select *
  2   from TABLE(GET_KEY_VALUES('{"A":"AA", "B":"BB", "C":"CC"}'))
  3  /
A                                AA
B                                BB
C                                CC

SQL>

Does this help




回答2:


Here's a variant that will walk a nested structure.

SQL> drop FUNCTION PROCESS_JSON_DOCUMENT
  2  /

Function dropped.

SQL> drop TYPE NV_PAIR_TABLE
  2  /

Type dropped.

SQL> drop TYPE NV_PAIR_T
  2  /

Type dropped.

SQL> create or replace TYPE NV_PAIR_T as object (
  2    JSON_PATH  VARCHAR2(4000),
  3    VALUE      VARCHAR2(4000)
  4  )
  5  /

Type created.

SQL> create or replace TYPE NV_PAIR_TABLE
  2  as TABLE of NV_PAIR_T
  3  /

Type created.

SQL> create or replace FUNCTION PROCESS_JSON_DOCUMENT(P_JSON_PATH VARCHAR2, P_JSON_DOCUMENT VARCHAR2)
  2  return NV_PAIR_TABLE PIPELINED
  3  as
  4    V_JSON_OBJECT    JSON_OBJECT_T := JSON_OBJECT_T(P_JSON_DOCUMENT);
  5    V_KEY_LIST       JSON_KEY_LIST := V_JSON_OBJECT.get_keys();
  6    V_KEY_NAME       VARCHAR2(4000);
  7    V_JSON_PATH      VARCHAR2(4000);
  8    V_CHILD_DOCUMENT VARCHAR2(4000);
  9  begin
 10    for i in 1..V_KEY_LIST.count loop
 11      V_KEY_NAME := V_KEY_LIST(i);
 12      if (V_JSON_OBJECT.get_type(V_KEY_LIST(i)) <> 'OBJECT') then
 13          pipe row (NV_PAIR_T(P_JSON_PATH || '.' || V_KEY_NAME,V_JSON_OBJECT.get_string(V_KEY_NAME)));
 14      else
 15        V_JSON_PATH := P_JSON_PATH || '.' || V_KEY_NAME;
 16        V_CHILD_DOCUMENT := V_JSON_OBJECT.get_object(V_KEY_NAME).to_string();
 17        for j in (select * from TABLE(PROCESS_JSON_DOCUMENT(V_JSON_PATH, V_CHILD_DOCUMENT))) loop
 18          pipe row (NV_PAIR_T(J.JSON_PATH,J.VALUE));
 19        end loop;
 20      end if;
 21    end loop;
 22  end;
 23  /

Function created.

SQL> column JSON_PATH format A32
SQL> column VALUE format A32
SQL> select *
  2   from TABLE(PROCESS_JSON_DOCUMENT('$','{"A":"AA", "B":"BB", "C":"CC", "X" : {"A":"AA", "B":"BB", "C":"CC"}}'))
  3  /
$.A                              AA
$.B                              BB
$.C                              CC
$.X.A                            AA
$.X.B                            BB
$.X.C                            CC

6 rows selected.

SQL>


来源:https://stackoverflow.com/questions/42702150/oracle-json-to-key-value-pair-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!