0

Currently i have two for loops that push ids into two separate json_array_t such as

[123,124,125,126]
[123,126,127]

I want to combine these two json_array_t into one and remove duplicates so it results in

[123,124,125,126,127]

to push ids from one json_array_t to another im doing this

ja_id1.put(1,id2,false);

But this puts all ids into one and keeps duplicates, and im not sure how to remove duplicates, thanks.

1 Answer 1

1

Check for duplicates before adding the value:

DECLARE 
  arr1 JSON_ARRAY_T := JSON_ARRAY_T.PARSE('[123,124,125,126]');
  arr2 JSON_ARRAY_T := JSON_ARRAY_T.PARSE('[123,126,127]');
BEGIN
  <<outer_loop>>
  FOR i IN 0 .. arr2.get_Size - 1
  LOOP
    FOR j IN 0 .. arr1.get_size - 1
    LOOP
      IF arr1.get_number(j) = arr2.get_number(i) THEN
        CONTINUE outer_loop;
      END IF;
    END LOOP;
    arr1.append(arr2.get_number(i));
  END LOOP;

  FOR i IN 0 .. arr1.get_Size - 1
  LOOP
    DBMS_OUTPUT.PUT_LINE(arr1.get_number(i));
  END LOOP;
END;
/

Which outputs:

123
124
125
126
127

db<>fiddle here

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer, this works perfectly, another question what about if its a json_array_t but instead of numbers would be objects and you would want to remove duplicates would it work similarly to your answer?
@user17983357 You would need to write something to check if one object's key-value pairs are identical to another object's key-value pairs (and if those objects contain nested objects/arrays then it may need to be recursive). If you have that then you can just use similar code and drop in the object comparator rather than comparing two numbers using =.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.