有没有一种方法可以使用Oracle SQL创建一个干净的JSON文件?

dldeef67  于 2023-03-24  发布在  Oracle
关注(0)|答案(1)|浏览(76)

我正在使用MS Report Builder,同时在Oracle 19 c上,将SQL查询转换为JSON。
下面是我的SQL代码:

SELECT json_object(
'id'        VALUE   REFVAL,
'recdate'   VALUE   DATEAPRECV,
'apptype'   VALUE   APPTYP,
'status'    VALUE   STAT,
'person'    VALUE   NAME
)

FROM APPLICATIONS

WHERE APPTYP = 'S10'

结果是:
| 报告|
| - ------|
| {“id”:“1”,“recdate”:“01-01-01”,“apptype”:“S10”,“status”:“COMP”,“person”:约翰|
| {“id”:“2”,“recdate”:“02-02-02”,“apptype”:“S10”,“status”:“REG”、“人员”:玛丽|
它产生的表格格式并不理想。
理想情况下,应该是这样的。

{"id" : "1", "recdate" : "01-01-01", "apptype" : "S10", "status" : "COMP", "person" : "John"},{"id" : "2", "recdate" : "02-02-02", "apptype" : "S10", "status" : "REG", "person" : "Mary"}

或者

[
   {
    "id" : "1",
    "recdate" : "01-01-01",
    "apptype" : "S10",
    "status" : "COMP",
    "person" : "John"
   },
   {
    "id" : "2",
    "recdate" : "02-02-02",
    "apptype" : "S10",
    "status" : "REG",
    "person" : "Mary"}
]

有没有办法复制这个?

bpsygsoo

bpsygsoo1#

可以使用JSON_ARRAYAGG()将所有JSON对象聚合到一个数组中:

select json_arrayagg(
    json_object(
        'id'        value   refval,
        'recdate'   value   dateaprecv,
        'apptype'   value   apptyp,
        'status'    value   stat,
        'person'    value   name
        )
    format json order by refval
) as agg
from applications
where apptyp = 'INVOICE'

相关问题