简体   繁体   English

Oracle SQL中的多行数据有没有办法使用JSON数组?

[英]Is there a way to use a JSON array for multi-row data in Oracle SQL?

Edit: @mathguy has already correctly pointed out that I need to use JSON_ARRAYAGG to have this correctly handle multi-row data.编辑:@mathguy 已经正确指出我需要使用 JSON_ARRAYAGG 来正确处理多行数据。 However, I still have an outstanding issue with the lastName object not returning speechmarks.但是,我仍然有一个未解决的问题,即 lastName object 没有返回语音标记。 Can anyone advise why this might be, from the SQL below?任何人都可以从下面的 SQL 中建议为什么会这样吗? Many thanks.非常感谢。

I have a requirement to generate JSON to insert data for lots of customers via an API. This contains JSON arrays (one overarching array for all customers and an array for potential multiple addresses) and objects.我需要生成 JSON 以通过 API 为大量客户插入数据。这包含 JSON arrays(一个用于所有客户的总体数组和一个用于潜在多个地址的数组)和对象。 The code I'm currently using produces this:我目前使用的代码产生了这个:

    [
    {
        "address": [
            {
                "addressLine1": "ALLIANCE & LEICESTER PLC",
                "addressLine2": "CUSTOMER SERVICES",
                "addressLine3": "CARLTON PARK",
                "region": "LEICESTERSHIRE",
                "city": "LEICESTER",
                "zip": "LE190AL",
                "type": "residential"
            },
            {
                "addressLine1": null,
                "addressLine2": null,
                "addressLine3": null,
                "region": null,
                "city": null,
                "zip": null,
                "type": null
            }
        ],
        "firstName": "SIOBHAN",
        "lastName":TOWNSEND
    }
]
[
    {
        "address": [
            {
                "addressLine1": "VIA DE LOS POBLADOS 2",
                "addressLine2": null,
                "addressLine3": null,
                "region": null,
                "city": "MADRID",
                "zip": "28033",
                "type": "residential"
            },
            {
                "addressLine1": null,
                "addressLine2": null,
                "addressLine3": null,
                "region": null,
                "city": null,
                "zip": null,
                "type": null
            }
        ],
        "firstName": "HAYDEN",
        "lastName":THOMSON
    }
]
[
    {
        "address": [
            {
                "addressLine1": "VIA DE LOS POBLADOS 1",
                "addressLine2": null,
                "addressLine3": null,
                "region": null,
                "city": "MADRID",
                "zip": "28034",
                "type": "residential"
            },
            {
                "addressLine1": null,
                "addressLine2": null,
                "addressLine3": null,
                "region": null,
                "city": null,
                "zip": null,
                "type": null
            }
        ],
        "firstName": "MADISON",
        "lastName":FROST
    }
]

...however I need it to look like this: ...但是我需要它看起来像这样:

[
    {
        "address": [
            {
                "addressLine1": "ALLIANCE & LEICESTER PLC",
                "addressLine2": "CUSTOMER SERVICES",
                "addressLine3": "CARLTON PARK",
                "region": "LEICESTERSHIRE",
                "city": "LEICESTER",
                "zip": "LE190AL",
                "type": "residential"
            },
            {
                "addressLine1": null,
                "addressLine2": null,
                "addressLine3": null,
                "region": null,
                "city": null,
                "zip": null,
                "type": null
            }
        ],
        "firstName": "SIOBHAN",
        "lastName":  "TOWNSEND"
    },
    {
        "address": [
            {
                "addressLine1": "VIA DE LOS POBLADOS 2",
                "addressLine2": null,
                "addressLine3": null,
                "region": null,
                "city": "MADRID",
                "zip": "28033",
                "type": "residential"
            },
            {
                "addressLine1": null,
                "addressLine2": null,
                "addressLine3": null,
                "region": null,
                "city": null,
                "zip": null,
                "type": null
            }
        ],
        "firstName": "HAYDEN",
        "lastName":  "THOMSON"
    },
    {
        "address": [
            {
                "addressLine1": "VIA DE LOS POBLADOS 1",
                "addressLine2": null,
                "addressLine3": null,
                "region": null,
                "city": "MADRID",
                "zip": "28034",
                "type": "residential"
            },
            {
                "addressLine1": null,
                "addressLine2": null,
                "addressLine3": null,
                "region": null,
                "city": null,
                "zip": null,
                "type": null
            }
        ],
        "firstName": "MADISON",
        "lastName":  "FROST"
    }
]

The key differences here being:这里的主要区别是:

  • The overarching array should start at the beginning of the 1st record and finish at the end of the last record, rather than ending after every "row"总体数组应从第一条记录的开头开始,到最后一条记录的末尾结束,而不是在每个“行”之后结束
  • For some reason, the last object in the list (lastName) doesn't have its value correctly listed in speechmarks出于某种原因,列表中的最后一个 object (lastName) 没有在 speechmarks 中正确列出其值
  • A comma separator should appear between each customer/row每个客户/行之间应出现逗号分隔符

This is the SQL I have been running:这是我一直在运行的 SQL:

SELECT
    json_array(
    json_object('address' VALUE
        json_array(json_object('addressLine1'    VALUE address.line_1,
                               'addressLine2'    VALUE address.line_2,
                               'addressLine3'    VALUE address.line_3,
                               'region'          VALUE address.county,
                               'city'            VALUE address.town,
                               'zip'             VALUE address.postcode,
                               'type'            VALUE 'residential'),
                   json_object('addressLine1'    VALUE correspondence_address.line_1,
                               'addressLine2'    VALUE correspondence_address.line_2,
                               'addressLine3'    VALUE correspondence_address.line_3,
                               'region'          VALUE correspondence_address.county,
                               'city'            VALUE correspondence_address.town,
                               'zip'             VALUE correspondence_address.postcode,
                               'type'            VALUE case when person.correspondence_address_id is null then null else 'correspondence' end)
                   ),
                'firstName'          VALUE person.first_name,
                'lastName'           VALUE person.surname
     FORMAT JSON)
              )as customer_json
FROM
    person,
    address,
    address correspondence_address
WHERE
    person.address_id=address.id
    and person.correspondence_address_id=correspondence_address.id(+)

This is being run against an Oracle 19c database.这是针对 Oracle 19c 数据库运行的。 Can anyone help me to troubleshoot if it's possible to set a JSON_ARRAY to wrap around the full dataset, rather than it ending and restarting after each customer record?如果可以设置一个 JSON_ARRAY 来环绕整个数据集,而不是在每个客户记录后结束并重新启动,谁能帮我解决问题?

Credit to @mathguy for correctly pointing out that I needed to use JSON_ARRAYAGG in order to get the SQL to correctly handle multiple rows.感谢@mathguy 正确指出我需要使用 JSON_ARRAYAGG 才能使 SQL 正确处理多行。

I have realised that I needed to remove the "FORMAT JSON" from the end as this trailed from the lastName variable and impacted the way it was returned.我意识到我需要从末尾删除“FORMAT JSON”,因为它尾随 lastName 变量并影响它的返回方式。 Removing this fixed my issue.删除它解决了我的问题。 Here is the working code:这是工作代码:

SELECT
json_arrayagg(json_object('address' VALUE
    json_array(json_object('addressLine1'    VALUE address.line_1,
                           'addressLine2'    VALUE address.line_2,
                           'addressLine3'    VALUE address.line_3,
                           'region'          VALUE address.county,
                           'city'            VALUE address.town,
                           'zip'             VALUE address.postcode,
                           'type'            VALUE 'residential'),
               json_object('addressLine1'    VALUE correspondence_address.line_1,
                           'addressLine2'    VALUE correspondence_address.line_2,
                           'addressLine3'    VALUE correspondence_address.line_3,
                           'region'          VALUE correspondence_address.county,
                           'city'            VALUE correspondence_address.town,
                           'zip'             VALUE correspondence_address.postcode,
                           'type'            VALUE case when person.correspondence_address_id is null then null else 'correspondence' end)
               ),
            'firstName'          VALUE person.first_name,
            'lastName'           VALUE person.surname
          ))as customer_json

FROM person, address, address correspondence_address WHERE person.address_id=address.id and person.correspondence_address_id=correspondence_address.id(+) FROM 人,地址,地址 correspondence_address WHERE person.address_id=address.id and person.correspondence_address_id=correspondence_address.id(+)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM