如何将当前JSON_OBJECT解析成列表形式

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

我很难理解如何在查询中切换结果。

我有以下片段运行到 Oracle 数据库中:

TO_NCLOB(JSON_OBJECT(
            'name' VALUE TRIM(tb1.DS_NAME),
            'birth' VALUE tb2.DT_BIRTH,
            'id' VALUE TRIM(tb2.P_ID),
            'email' VALUE tb3.DS_EMAIL,
            'phone' VALUE tb3.NUM_PHONE,
            'add' VALUE (SELECT JSON_ARRAYAGG(TRIM(tb4.ADD))
                FROM SCHEMA1.TABLE_4 tb4
                INNER JOIN SCHEMA1.TABLE_5 tb5
                    ON tb6.ID = tb5.ID
                WHERE tb4.ID_F = tb5.ID_F),
            'formula' VALUE (SELECT JSON_ARRAYAGG(TRIM(tb4.FORM))
                FROM SCHEMA1.TABLE_4 tb4
                INNER JOIN SCHEMA1.TABLE_5 tb5
                    ON tb6.ID = tb6.ID_U
                WHERE tb4.ID_FORM = tb5.ID_FORM),
            'code' VALUE (SELECT JSON_ARRAYAGG(TRIM(tb7.CODE))
                FROM SCHEMA1.TABLE_4 tb4
                INNER JOIN SCHEMA1.TABLE_5 tb5
                    ON tb6.ID = tb5.ID_U
                INNER JOIN SCHEMA1.TABLE_8 tb8
                    ON tb5.ID_E = tb8.ID_E
                INNER JOIN SCHEMA1.TABLE_7 tb7
                    ON tb8.ID_UN = tb7.ID_P))) AS USER_INFO

我需要转换当前的输出,如下所示:

{
    "name": "JOSEPH STANLY",
    "id": "5821451552112234",
    "email": "[email protected]",
    "phone": "3585445551451155",
    "birth": "1980-01-29",
    "add": [
        "255521",
        "357441"
    ],
    "formula": [
        "Architect",
        "Interior Designer"
    ],
    "code": [
        "8955445",
        "1225587"
    ]
}

进入这样的事情:

[
    {
        "name": "JOSEPH STANLY",
        "id": "5821451552112234",
        "email": "[email protected]",
        "phone": "3585445551451155",
        "birth": "1980-01-29",
        "add": "223505",
        "formula": "Architect",
        "code": "8955445",
    },
    {
        "name": "JOSEPH STANLY",
        "id": "5821451552112234",
        "email": "[email protected]",
        "phone": "3585445551451155",
        "birth": "1980-01-29",
        "add": "1980-01-29",
        "formula": "Interior Designer",
        "code": "1225587",
    }
]

如您所见,组成字段

USER_INFO
的某些字段具有多种信息。我怎样才能像上面的例子那样拆分?

我尝试删除

JSON_ARRAYAGG
中的
SELECT
,但这不起作用。

sql arrays json oracle oracle11g
1个回答
0
投票
with data(js) as (
    select q'~{
    "name": "JOSEPH STANLY",
    "id": "5821451552112234",
    "email": "[email protected]",
    "phone": "3585445551451155",
    "birth": "1980-01-29",
    "add": [
        "255521",
        "357441"
    ],
    "formula": [
        "Architect",
        "Interior Designer"
    ],
    "code": [
        "8955445",
        "1225587"
    ]
}~'
)
select 
json_arrayagg(
    json_object(
     ta.name, ta.id, ta.email, ta.phone, ta.birth,
     "add", formula, code
     )
    )
from 
data d,
json_table(
    js,
    '$'
    columns (
        name path '$.name',
        id path '$.id',
        email path '$.email',
        phone path '$.phone',
        birth path '$.birth',
        nested path '$.add[*]'
        columns (
            idx for ordinality,
            "add" path '$'
        )
    )
) ta
left join 
json_table(
    js,
    '$'
    columns (
        id path '$.id',
        nested path '$.formula[*]'
        columns (
            idx for ordinality,
            formula path '$'
        )
    )
) tf on ta.id = tf.id and ta.idx = tf.idx
left join json_table(
    js,
    '$'
    columns (
        id path '$.id',
        nested path '$.code[*]'
        columns (
            idx for ordinality,
            code path '$'
        )
    )
) tc on ta.id = tc.id and ta.idx = tc.idx
;




   [
{"name":"JOSEPH STANLY","id":"5821451552112234","email":"[email protected]","phone":"3585445551451155","birth":"1980-01-29","add":"255521","formula":"Architect","code":"8955445"},
{"name":"JOSEPH STANLY","id":"5821451552112234","email":"[email protected]","phone":"3585445551451155","birth":"1980-01-29","add":"357441","formula":"Interior Designer","code":"1225587"}
]
© www.soinside.com 2019 - 2024. All rights reserved.