2014-11-25 21 views
11

ho creato il seguente script per leggere i dati da Mobile App DB (che si basa su MongoDB) da Oracle SQL Developer:Parse JSON nella tabella di Oracle utilizzando PL/SQL

 
DECLARE 
    l_param_list  VARCHAR2(512); 

    l_http_request UTL_HTTP.req; 
    l_http_response UTL_HTTP.resp; 

    l_response_text VARCHAR2(32767); 
BEGIN 

    -- service's input parameters 

    -- preparing Request... 
    l_http_request := UTL_HTTP.begin_request ('https://api.appery.io/rest/1/db/collections/Photos?where=%7B%22Oracle_Flag%22%3A%22Y%22%7D' 
              , 'GET' 
              , 'HTTP/1.1'); 

    -- ...set header's attributes 
    UTL_HTTP.set_header(l_http_request, 'X-Appery-Database-Id', '53f2dac5e4b02cca64021dbe'); 
    --UTL_HTTP.set_header(l_http_request, 'Content-Length', LENGTH(l_param_list)); 

    -- ...set input parameters 
-- UTL_HTTP.write_text(l_http_request, l_param_list); 

    -- get Response and obtain received value 
    l_http_response := UTL_HTTP.get_response(l_http_request); 

    UTL_HTTP.read_text(l_http_response, l_response_text); 

    DBMS_OUTPUT.put_line(l_response_text); 
    insert into appery values(l_response_text); 
    -- finalizing 
    UTL_HTTP.end_response(l_http_response); 


EXCEPTION 
    WHEN UTL_HTTP.end_of_body 
    THEN UTL_HTTP.end_response(l_http_response); 
END; 
/

La risposta (l_response_text) è una stringa simile a JSON. Ad esempio:

[{"Postcode":"47100","OutletCode":"128039251","MobileNumber":"","_createdAt":"2014-11-10 06:12:49.837","_updatedAt":"2014-11-10 06:12:49.837"}, {"Postcode":"32100","OutletCode":"118034251", ..... ]

Il codice funziona bene, e inserisce la risposta in una colonna tavolo chiamato appery. Tuttavia, ho bisogno di analizzare questa risposta in modo tale che ogni array entri nella sua specifica colonna in una tabella chiamata appery_test. La tabella appery_test ha un numero di colonne uguale al numero di coppie JSON e nello stesso ordine.

Ho cercato e ho trovato la maggior parte dei risultati sull'analisi del tavolo Oracle in JSON e non il contrario. Ho trovato, però, il collegamento this che è in qualche modo simile al mio problema. Tuttavia, la libreria suggerita nella risposta non ha alcun esempio su come usarla per inserire JSON nella tabella convenzionale usando PL/SQL.

N.B .: Sto usando 11g e non 12c. Quindi le funzioni built in non sono disponibili per me.

risposta

8

Ho usato PL/JSON library. In particolare, il pacchetto JSON_EXT funziona per analizzarlo.

Il seguente script ispirato risposta Oracle Community ha funzionato per me

 
DECLARE 
    l_param_list  VARCHAR2(512); 

    l_http_request UTL_HTTP.req; 
    l_http_response UTL_HTTP.resp; 

    l_response_text VARCHAR2(32767); 

l_list json_list; 
A_id   VARCHAR2(200); 
UserId   VARCHAR2(100); 
UserName  VARCHAR2(100); 
OutletCode  VARCHAR2(100); 
OutletName  VARCHAR2(100); 
MobileNumber VARCHAR2(100); 
PhoneNumber VARCHAR2(100); 
Address  VARCHAR2(100); 
City   VARCHAR2(100); 
State   VARCHAR2(100); 
Postcode  VARCHAR2(100); 
Email   VARCHAR2(100); 
UpdateCount VARCHAR2(100); 
loginCount  VARCHAR2(100); 
ReferencePhoto VARCHAR2(100); 
Updates  VARCHAR2(100); 
AccountLocked VARCHAR2(100); 
Oracle_Flag VARCHAR2(100); 
acl   VARCHAR2(100); 


BEGIN 

    -- service's input parameters 

    -- preparing Request... 
    l_http_request := UTL_HTTP.begin_request('https://api.appery.io/rest/1/db/collections/Outlet_Details?where=%7B%22Oracle_Flag%22%3A%22Y%22%7D' 
              , 'GET' 
              , 'HTTP/1.1'); 

    -- ...set header's attributes 
    UTL_HTTP.set_header(l_http_request, 'X-Appery-Database-Id', '53f2dac5e4b02cca64021dbe'); 
    --UTL_HTTP.set_header(l_http_request, 'Content-Length', LENGTH(l_param_list)); 

    -- ...set input parameters 
-- UTL_HTTP.write_text(l_http_request, l_param_list); 

    -- get Response and obtain received value 
    l_http_response := UTL_HTTP.get_response(l_http_request); 

    UTL_HTTP.read_text(l_http_response, l_response_text); 

    DBMS_OUTPUT.put_line(l_response_text); 
    l_list := json_list(l_response_text); 

FOR i IN 1..l_list.count 
LOOP 
    A_id   := json_ext.get_string(json(l_list.get(i)),'_id'); 
    UserId   := json_ext.get_string(json(l_list.get(i)),'UserId'); 
    UserName  := json_ext.get_string(json(l_list.get(i)),'UserName'); 
    OutletCode  := json_ext.get_string(json(l_list.get(i)),'OutletCode'); 
    OutletName  := json_ext.get_string(json(l_list.get(i)),'OutletName'); 
    MobileNumber := json_ext.get_string(json(l_list.get(i)),'MobileNumber'); 
    PhoneNumber := json_ext.get_string(json(l_list.get(i)),'PhoneNumber'); 
    Address  := json_ext.get_string(json(l_list.get(i)),'Address'); 
    City   := json_ext.get_string(json(l_list.get(i)),'City'); 
    State   := json_ext.get_string(json(l_list.get(i)),'State'); 
    Postcode  := json_ext.get_string(json(l_list.get(i)),'Postcode'); 
    Email   := json_ext.get_string(json(l_list.get(i)),'Email'); 
    UpdateCount := json_ext.get_string(json(l_list.get(i)),'UpdateCount'); 
    loginCount  := json_ext.get_string(json(l_list.get(i)),'loginCount'); 
    ReferencePhoto := json_ext.get_string(json(l_list.get(i)),'ReferencePhoto'); 
    Updates  := json_ext.get_string(json(l_list.get(i)),'Updates'); 
    AccountLocked := json_ext.get_string(json(l_list.get(i)),'AccountLocked'); 
    Oracle_Flag := json_ext.get_string(json(l_list.get(i)),'Oracle_Flag'); 
    acl   := json_ext.get_string(json(l_list.get(i)),'acl'); 


insert ..... 

noti che json_ext.get_string Retuns solo VARCHAR2 limitato a 32767 max. Per utilizzare lo stesso pacchetto con json_list e json_values ​​più grandi (> 32 KB), selezionare here.

12

Poiché tale questione i punteggi più alti nei risultati, voglio pubblicare questo alternativa preferita:

Oracle ha rilasciato APEX 5.0 (15 aprile 2015). Con esso si ottiene l'accesso a una grande API per lavorare con JSON

Lo sto usando su 11.2 e sono stato in grado di scricchiolare ogni singolo json, da oggetti semplici a molto complessi con array multipli e 4/5 livelli. APEX_JSON

Se non si desidera utilizzare APEX. Basta installare l'ambiente di runtime per ottenere l'accesso all'API.

utilizzo di esempio, i dati di json.org's example:

declare 
    sample_json varchar2 (32767) 
     := '{ 
    "glossary": { 
     "title": "example glossary", 
     "GlossDiv": { 
      "title": "S", 
      "GlossList": { 
       "GlossEntry": { 
        "ID": "SGML", 
        "SortAs": "SGML", 
        "GlossTerm": "Standard Generalized Markup Language", 
        "Acronym": "SGML", 
        "Abbrev": "ISO 8879:1986", 
        "GlossDef": { 
         "para": "A meta-markup language, used to create markup languages such as DocBook.", 
         "GlossSeeAlso": ["GML", "XML"] 
        }, 
        "GlossSee": "markup" 
       } 
      } 
     } 
    } 
}'; 
begin 
    apex_json.parse (sample_json); 
    dbms_output.put_line (apex_json.get_varchar2 ('glossary.GlossDiv.title')); 
    dbms_output.put_line (apex_json.get_varchar2 ('glossary.GlossDiv.GlossList.GlossEntry.GlossTerm')); 
    dbms_output.put_line (apex_json.get_varchar2 ('glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso[%d]', 2)); 
end; 

Risultato: blocco PL/SQL eseguito

S 
Standard Generalized Markup Language 
XML