Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Tricky output based on Hierarchy

Arif2018Aug 1 2021 — edited Aug 1 2021

i have a situation where i will be passing object to get the parent object , the challenge is there are 3 parents to that object , for example if i have object 4 and want to get object 3 it should get object 3 , if want to get parent object 2 it should get object 2 for object 4. For example , i will pass the child object along with level of parent then it should brng the right parent in hierarchy according to level of parent ,
[code]
create table test_object (mch_code varchar2(25),sup_mch_code varchar2(25), mch_code_level varchar2(20))

insert into test_object (mch_code,sup_mch_code , mch_code_level) values ('AAA-AAA-AAA-AA','AAA-AAA-AAA','L:5');
insert into test_object (mch_code,sup_mch_code , mch_code_level) values ('AAA-AAA-AAA','AAA-AAA','L:4');
insert into test_object (mch_code,sup_mch_code , mch_code_level) values ('AAA-AAA','AAA','L:3');
insert into test_object (mch_code,sup_mch_code , mch_code_level) values ('AAA','A','L:2');
insert into test_object (mch_code,sup_mch_code , mch_code_level) values ('BBB-BBB-BBB-BB','BBB-BBB-BBB','L:5');
insert into test_object (mch_code,sup_mch_code , mch_code_level) values ('BBB-BBB-BBB','BBB-BBB','L:4');
insert into test_object (mch_code,sup_mch_code , mch_code_level) values ('BBB-BBB','BBB','L:3');
insert into test_object (mch_code,sup_mch_code , mch_code_level) values ('BBB','B','L:2');

--get ancestor object
select get_ancestor_object('AAA-AAA-AAA','L:2') from dual;
--first is the object , second parameter is the level of parent that i want.
should return 'AAA'
select get_ancestor_object('AAA-AAA-AAA-AA,'L:3') from dual;
should return 'AAA-AAA'
[/CODE]

This post has been answered by Solomon Yakobson on Aug 1 2021
Jump to Answer

Comments

aroumeli

Hi,
We require the same: returning raw json from pl/sql procedure. Did you find a solution?

Aaron L.

Hope an amateur like me can help......
To POST json to a clob column we do the following:

INSERT INTO js_table
 (js_column)
VALUES
 (blob_to_clob(:body));

oracle-base.com has a version of the blob_to_clob sql
If you just want to return the json clob, the SELECT-statement User_Z87BQ works for us.
We have pl/sql that build the json by using something like the following:

BEGIN
  APEX_JSON.OPEN_OBJECT;
  APEX_JSON.WRITE('result','success');
  APEX_JSON.WRITE('message','hello world');
  APEX_JSON.CLOSE_OBJECT;
END;
thatJeffSmith-Oracle

just print it with htp.p

image.png

aroumeli

That's brilliant Jeff, thank you!
I'm using:
OWA_UTIL.mime_header('application/json', TRUE);
in order to set the correct content-type. Is this the correct way to go?

User_2JVWU

For anyone having the same issue: you could simply return the column which holds JSON as a plain JSON type by using a column name like "{}columnname", see:

SELECT id,
       json "{}json"
  FROM table_with_json

ref: https://www.thatjeffsmith.com/archive/2017/09/ords-returning-raw-json/

User_2JVWU

For anyone having the same issue: you could simply return the column which holds JSON as a plain JSON type by using a column name like "{}columnname", see:

SELECT id,
       json "{}json"
  FROM table_with_json

ref: https://www.thatjeffsmith.com/archive/2017/09/ords-returning-raw-json/

Olafur T

Create GET handler, type PL/SQL
Some examples of creating output:

begin
  owa_util.status_line(201, '', false);
  owa_util.mime_header('application/json', true);
  htp.prn('{"status": "Item created successfully"}');
end;

--

begin
  owa_util.status_line(200, '', false);
  owa_util.mime_header('application/json', true);
  htp.prn('{"items":[');
  for i in (select rownum as rn, object_Type, object_name, status from all_objects where owner = 'ORDS_METADATA' fetch next 10 rows only) loop
    if i.rn > 1 then
      htp.prn(',');
    end if;
    htp.prn('{"type": "' || i.object_type || '"');
    htp.prn(',"name": "' || i.object_name || '"');
    htp.prn(',"status": "' || i.status || '"}');
  end loop;
  htp.prn(']}');
end;

--

declare
  l_arr json_array_t := json_array_t();
begin
  for i in (select object_Type, object_name, status from all_objects where owner = 'ORDS_METADATA' fetch next 10 rows only) loop
    declare
      l_obj json_object_t := json_object_t();
    begin
      l_obj.put('type', i.object_type);
      l_obj.put('name', i.object_name);
      l_obj.put('status', i.status);
      l_arr.append(l_obj);
    end;
  end loop;
  owa_util.status_line(200, '', false);
  owa_util.mime_header('application/json', true);
  apex_util.prn(l_arr.to_clob);
end;

Regards
Oli

Olafur T

And for posting your CLOB, Jeff has already shown how to do a GET media resource.
This is an alternative way to do POST in PL/SQL, I'm assuming it's an array, change as needed:

 begin
  owa_util.status_line(200, '', false);
  owa_util.mime_header('application/json', true);
  htp.prn('{"items":');
  for i in (select jsonclob from mytable where id = :id) loop
    apex_util.prn(i.jsonclob);
  end loop; 
end;

of course under production, this would be a select into with a no_data_found handler

1 - 8

Post Details

Added on Aug 1 2021
4 comments
76 views