将MySQL结果集转换为单个JSON OBJECT(MySQL数据类型)

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

Background

我在Oracle Integration Cloud中创建了一个REST端点,它将id作为模板参数(例如:http://endpoint.com/api/orders/{id}),调用MySQL中的存储过程(传入相同的id参数)返回数据,然后进行转换然后将该数据作为响应有效负载中的JSON。

该工具有点限制性,如果存储过程可以将查询结果转换并输出为JSON(主要使用MySQL的新JSON_OBJECTJSON_ARRAY等函数),它将使我的生活更轻松。它实际上只需要采用正确的JSON格式(IE中MySQL的响应的实际内容类型无关紧要)。

我已尽力尽可能彻底地避免混淆/在评论中来回讨论,但是如果这个例子很长,你可以跳到最后一节,这可能就足够了。

Outline of Current Process

响应有效内容具有以下JSON模式

{
  "$id": "http://example.com/example.json",
  "type": "object",
  "definitions": {},
  "$schema": "http://json-schema.org/draft-07/schema#",
  "properties": {
    "results": {
      "$id": "/properties/results",
      "type": "array",
      "items": {
        "$id": "/properties/results/items",
        "type": "object",
        "properties": {
          "requestId": {
            "$id": "/properties/results/items/properties/requestId",
            "type": "string",
            "title": "The Requestid Schema ",
            "default": "",
            "examples": [
              "id"
            ]
          },
          "invoiceNumber": {
            "$id": "/properties/results/items/properties/invoiceNumber",
            "type": "string",
            "title": "The Invoicenumber Schema ",
            "default": "",
            "examples": [
              "129"
            ]
          },
          "orderNumber": {
            "$id": "/properties/results/items/properties/orderNumber",
            "type": "string",
            "title": "The Ordernumber Schema ",
            "default": "",
            "examples": [
              "13"
            ]
          }
        }
      }
    }
  }
}

这是一个示例响应有效负载

{
  "results": [
    {
      "requestId": "2018-CRD-1818",
      "invoiceNumber": "120129",
      "orderNumber": "1343"
    },
    {
      "requestId": "2018-RMA-1891",
      "invoiceNumber": "1291234
      "orderNumber": "1323"
    }
  ]
}

目前,我有一个存储过程,它基本上将多行压缩成一个逗号分隔的行,然后将结果返回给Oracle的Integration Cloud。

 DELIMITER //
 CREATE PROCEDURE GetOrderNumbers(IN id int, OUT ids varchar(4000))
   BEGIN

        SELECT 
                group_concat(concat(crd.requestId, ',', crd.originalOrderNumber))
            FROM 
                LathamCR_CreditRequest crd
            WHERE 
                crd.originalOrderNumber = id
            UNION ALL
            SELECT 
                rma.requestId
            FROM 
                LathamCR_RMARequest rma
            WHERE 
                rma.originalOrderNumber = id
            LIMIT 1
        INTO ids;
    END //
DELIMITER ;

然后在OIC中我将csv转换为前面提到的JSON并返回它。

New Process and Question

如前所述,我不想从存储过程返回单行CSV,而是返回一个JSON对象(基本上是在MySQL中进行转换)。所以存储过程签名如下

 DELIMITER //
 CREATE PROCEDURE GetOrderNumbers(IN id int, OUT ids JSON)
   BEGIN

         # The SQL Transformation

    END //
DELIMITER ;

我遇到的问题是我似乎无法将多行压缩成单个JSON对象。我可以使用以下内容将每一行压缩为JSON对象:

SELECT
    JSON_OBJECT('requestId',crd.requestId, 'invoiceNumber', crd.originalOrderInvoice, 'orderNumber', crd.originalOrderNumber)
FROM 
    LathamCR_CreditRequest crd
UNION ALL
SELECT 
    JSON_OBJECT('requestId',rma.requestId, 'invoiceNumber', rma.originalOrderInvoice, 'orderNumber', rma.originalOrderNumber)
FROM
    LathamCR_RMARequest rma;

这将返回以下结果集(其中每个新行都是一行)。

# results
'{\"requestId\": \"2015-CRD-0000001\", \"orderNumber\": \"35454\", \"invoiceNumber\": \"3654654\"}'
'{\"requestId\": \"2015-CRD-0000002\", \"orderNumber\": \"4343434\", \"invoiceNumber\": \"3243434\"}'
'{\"requestId\": \"2015-CRD-0000003\", \"orderNumber\": \"3423423\", \"invoiceNumber\": \"2342342\"}'
'{\"requestId\": \"2015-CRD-0000004\", \"orderNumber\": \"3543543\", \"invoiceNumber\": \"9766483\"}'
'{\"requestId\": \"2015-CRD-0000005\", \"orderNumber\": \"8754632\", \"invoiceNumber\": \"**77732\"}'

这正是我想要的,除了我想将一行中的所有内容作为父对象内的数组返回。例如:

{ 
   "results": [
      {"requestId": "2015-CRD-0000005", "orderNumber": "8754632", "invoiceNumber": "**77732"}, 
      {"requestId": "2016-CRD-0000005", "orderNumber": "866632", "invoiceNumber": "*6732"}
    ]
}

因此,为了尝试将它连成一行,我使用GROUP_CONCATJSON_ARRAY如下:

SELECT
    GROUP_CONCAT(
        JSON_ARRAY (
            JSON_OBJECT('requestId',crd.requestId, 'invoiceNumber', crd.originalOrderInvoice, 'orderNumber', crd.originalOrderNumber)
        )
    ) as result
FROM 
    LathamCR_CreditRequest crd
UNION ALL
SELECT 
    GROUP_CONCAT(
        JSON_ARRAY (
            JSON_OBJECT('requestId',rma.requestId, 'invoiceNumber', rma.createdDate, 'orderNumber', rma.originalOrderNumber)
        )
    ) as result
FROM
    LathamCR_RMARequest rma;

这非常接近,但它返回两行(union all基本上停止工作),而且我仍然需要将数组包装在父JSON对象中。当我尝试使用JSON_OBJECT作为最外面的块时,它然后将数组视为一个字符串(我仍然无法使union all再次工作)。

mysql arrays json etl
1个回答
1
投票

一种选择是使用JSON_MERGE()(deprecated 5.7.22) - > JSON_MERGE_PATCH() / JSON_MERGE_PRESERVE(),请参阅12.16.1 JSON Function Reference

SET @`json_LathamCR_CreditRequest` :=
  (SELECT
     CONCAT(
       '[',
       GROUP_CONCAT(
         JSON_OBJECT('requestId', `requestId`)
       ),
       ']')
  FROM `LathamCR_CreditRequest`);

SET @`json_LathamCR_RMARequest` :=
  (SELECT
     CONCAT(
       '[',
       GROUP_CONCAT(
         JSON_OBJECT('requestId', `requestId`)
       ),
       ']')
  FROM `LathamCR_RMARequest`);

SELECT
  `der`.`result`,
  JSON_VALID(`der`.`result`) `JSON_VALID?`
FROM (
  SELECT
    JSON_OBJECT(
      'results',
      JSON_MERGE(@`json_LathamCR_CreditRequest`,
                 @`json_LathamCR_RMARequest`
      )
  ) `result`
) `der`;

db-fiddle

© www.soinside.com 2019 - 2024. All rights reserved.