在 PLSQL 中返回 JSON_OBJECT_T 响应而不是 CLOB

问题描述 投票:0回答:1

我想返回 JSON OBJECT 但我有一个返回 CLOB 的代码,我如何修改下面以便返回 JSON_OBJECT_T 响应而不是 clob 。请帮忙

DECLARE
  p_json CLOB;
BEGIN
  SELECT JSON_SERIALIZE(
           JSON_OBJECT(
             KEY 'employees' VALUE JSON_ARRAYAGG(
               JSON_OBJECT(
                 KEY 'empno' VALUE id,
                 KEY 'ename' VALUE name,
                 KEY 'job'   VALUE email_id
               )
               ORDER BY id
             )
           )
           RETURNING CLOB
         ) AS json
  INTO   p_json
  FROM   employee_tbl;
  DBMS_OUTPUT.PUT_LINE(p_json);
END;
/

预期输出:

{"employees":[{"empno":1065,"ename":"Abu","job":"[email protected]"},{"empno":1066,"ename":"Umar","job":"[email protected]"}]}

样本数据:

CREATE TABLE employee_tbl (id, name, email_id) AS
  SELECT 1065, 'Abu',  '[email protected]' FROM DUAL UNION ALL
  SELECT 1066, 'Umar', '[email protected]' FROM DUAL
sql plsql oracle-sqldeveloper
1个回答
0
投票

只需从查询中删除 JSON_SERIALIZE,您就会得到所需的 JSON_OBJECT。

  SELECT JSON_OBJECT
           ( KEY 'employees' 
             VALUE JSON_ARRAYAGG
                   ( JSON_OBJECT( KEY 'empno' VALUE id,
                                  KEY 'ename' VALUE name,
                                  KEY 'job'   VALUE email_id
                                )
                    ORDER BY id
                  )
           )
         AS json
  FROM   employee_tbl;
/*      R e s u l t :
JSON
--------------------------------------------------------------------------------------------------------------------------------
{"employees":[{"empno":1065,"ename":"Abu","job":"[email protected]"},{"empno":1066,"ename":"Umar","job":"[email protected]"}]}    */
© www.soinside.com 2019 - 2024. All rights reserved.