0

编辑:@mathguy 已经正确指出我需要使用 JSON_ARRAYAGG 才能正确处理多行数据。但是,我仍然有一个未返回语音标记的 lastName 对象的突出问题。任何人都可以从下面的 SQL 中提出为什么会这样吗?非常感谢。

我需要生成 JSON 以通过 API 为大量客户插入数据。这包含 JSON 数组(一个用于所有客户的总体数组和一个用于潜在多个地址的数组)和对象。我目前使用的代码产生了这个:

    [
    {
        "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
    }
]

...但是我需要它看起来像这样:

[
    {
        "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"
    }
]

这里的主要区别是:

  • 总体数组应从第一条记录的开头开始并在最后一条记录的结尾结束,而不是在每个“行”之后结束
  • 由于某种原因,列表中的最后一个对象 (lastName) 没有在语音标记中正确列出其值
  • 每个客户/行之间应出现逗号分隔符

这是我一直在运行的 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(+)

这是针对 Oracle 19c 数据库运行的。如果可以设置 JSON_ARRAY 来环绕整个数据集,而不是在每个客户记录后结束并重新启动,谁能帮我解决问题?

4

1 回答 1

0

感谢@mathguy 正确地指出我需要使用 JSON_ARRAYAGG 才能让 SQL 正确处理多行。

我已经意识到我需要从末尾删除“FORMAT JSON”,因为它从 lastName 变量开始,并影响了它的返回方式。删除这个解决了我的问题。这是工作代码:

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 人、地址、地址correspondence_address WHERE person.address_id=address.id and person.correspondence_address_id=correspondence_address.id(+)

于 2022-02-25T15:20:20.823 回答