Forum Stats

  • 3,851,779 Users
  • 2,264,024 Discussions
  • 7,904,848 Comments

Discussions

how to combine two JSON array list into single array list

2»

Answers

  • Solomon Yakobson
    Solomon Yakobson Member Posts: 19,722 Red Diamond
    edited Mar 6, 2022 12:41PM

    And if you don't have var1/var2 but rather have whole json:

    declare
        v_json             varchar2(1000);
        v_temp_json_object json_object_t;
        v_temp_json_array  json_array_t;
        v_res_json_array   json_array_t;
        v_res_json_object  json_object_t:= json_object_t();
    begin
        v_json             := '{"tktKey":"123456","attlist1":[{"attkey":"123","attval":"abc"},{"attkey":"456","attval":"def"}],"attlist2":[{"attkey":"098","attval":"xyz"},{"attkey":"999","attval":"pqrs"}]}';
        v_temp_json_object := json_object_t(v_json);
        v_res_json_array   := v_temp_json_object.get_array('attlist1');
        v_temp_json_array  := v_temp_json_object.get_array('attlist2');
        for v_i in 0..v_temp_json_array.get_size - 1 loop
          v_res_json_array.append(v_temp_json_array.get(v_i));
        end loop;
        v_res_json_object.put('tktKey',v_temp_json_object.get('tktKey'));
        v_res_json_object.put('attlist',v_res_json_array);
        dbms_output.put_line(v_res_json_object.stringify);
    end;
    /
    
    {"tktKey":"123456","attlist":[{"attkey":"123","attval":"abc"},{"attkey":"456","attval":"def"},{"attkey":"098","attval":"xyz"},{"attkey":"999","attval":"pqrs"}]}
    
    PL/SQL procedure successfully completed.
    
    SQL>
    

    SY.