Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Gerben Jan.

Asked: July 04, 2019 - 10:32 am UTC

Last updated: July 04, 2019 - 2:34 pm UTC

Version: 19.1

Viewed 1000+ times

You Asked

Im trying to get JSON data from an api.

The JSON Data looks like this:

[
  1354830081,
  99005338,
  498125261,
  99003581,
  99003214,
  99008228,
  1900696668,
  99003006,
  99009268,
  1911932230,
  99007362,
  99004514,
  1727758877,
  99007044,
  131511956,
  1411711376,
  99002367,
  99002938,
  99004425,
  99008223,
  99009082,
  99003838,
  99007149,
  99004116,
  448511760,
  386292982,
  741557221,
  99003212,
  99007498,
  1988009451,
  1042504553,
  1220922756,
  99003995,
  117383987,
  99006828,
  154104258,
  99007203,
  99004901,
  937872513,
  150097440,
  99001954,
  99003714,
  1496500070,
  99008259,
  99007221,
  99003144,
  99006961,
  99008879,
  99008301,
  99001099,
  99003549,
  99008802,
  99005874,
  499005583,
  99007871,
  99002003,
  99005065,
  99002775,
  99007391,
  673381830,
  99008469,
  434243723,
  99002107,
  99008809,
  982284363,
  99001317,
  99001648,
  99006820,
  99007574,
  99008458,
  99006069,
  99007716,
  99008493,
  99005518,
  99009201,
  99007969,
  99004344,
  99008788,
  99007252,
  288377808,
  679584932,
  99005697,
  99004136,
  99008556,
  173714703,
  99005100,
  99005443,
  99001657,
  99006985,
  99007916,
  1681527727,
  99008976,
  99008826,
  99005688,
  707482380,
  99007289,
  99006941,
  99001134,
  99009163]


I so I am not getting an key, but only the id's in return.

I have tried the following:
      apex_json.parse(p_values => l_values, p_source => l_clob);  
      
      l_num := APEX_JSON.get_count(p_path => '.',p_values => l_values);
      DBMS_OUTPUT.put_line('Lines: '||l_num);
    FOR i IN 1 .. l_num
    LOOP
    v_data := apex_json.get_number('.$',i);
        --INSERT INTO TBL_ALL_ALLIANCES VALUES (v_data);         
    DBMS_OUTPUT.put_line('- Data: '||v_data);
    END LOOP;

   


But even with just the . in the apex_json.get_number i cannot get the value returned, I do however get the correct amount of rows from apex_json.get_number but i just seem to not get the correct value. Does anyone know how to get the value from the rows that don't have a key?

and Chris said...

You just reference the element of the array you want, e.g. [3]:

declare
  l_values varchar2(1000) := '[
  1354830081,
  99005338,
  498125261,
  99003581,
  99003214,
  99008228,
  1900696668,
  99003006,
  99009268,
  1911932230]';
  v_data int;
  l_num int;
begin

    apex_json.parse(l_values);  
      
    l_num := APEX_JSON.get_count(p_path => '.');
    DBMS_OUTPUT.put_line('Lines: '||l_num);
    FOR i IN 1 .. l_num
    LOOP
      v_data := apex_json.get_number('[' || i || ']',i);     
      DBMS_OUTPUT.put_line('- Data: '||v_data);
    END LOOP;
end;
/

Lines: 10
- Data: 1354830081
- Data: 99005338
- Data: 498125261
- Data: 99003581
- Data: 99003214
- Data: 99008228
- Data: 1900696668
- Data: 99003006
- Data: 99009268
- Data: 1911932230


From 12.1.0.2 you can also do this in SQL with JSON_table:

with rws as (
  select '[
  1354830081,
  99005338,
  498125261,
  99003581,
  99003214,
  99008228,
  1900696668,
  99003006,
  99009268,
  1911932230]' j 
  from dual
)
  select arr.*
  from   rws, json_table (
    j, '$'
    columns ( 
      nested path '$[*]' 
      columns (
        c1 int path '$'
      )
    )
  ) arr;

C1           
   1354830081 
     99005338 
    498125261 
     99003581 
     99003214 
     99008228 
   1900696668 
     99003006 
     99009268 
   1911932230 


Is this answer out of date? If it is, please let us know via a Comment

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.