使用 XQuery 以对象格式为单个记录而不是数组获取 JSON 输出

Getting the JSON output in an object format for a single record instead of an array using XQuery

我从 API 中得到 JSON 数组格式(对于多条记录)和对象格式(对于单个记录)的输出。相反,消费者希望维护一种格式——也作为单个记录的数组。请让我知道是否可以使用 XQuery

以数组格式显示 JSON 输出而不考虑 single/multiple 记录

我试过下面的 XQuery:

<Response>
 <totalSize>{totalSize/number()}</totalSize>
 <done>{done/text()}</done>
 <nextRecordsUrl>{nextRecordsUrl/text()}</nextRecordsUrl>
 {
 let $input:=/root/records
 for $i in $input
return
<records>
       <Product1>{$i/Product_Lookup_BI__c/text()}</Product1>
       <EventLastModifiedDate>{$i/LastModifiedDate/text()}</EventLastModifiedDate>
       <Venue>{$i/Venue_vod__r/(fn:concat(fn:concat(fn:concat(fn:concat(fn:concat(fn:concat(BI_Country_Code__c/text(),'-'),State_Province_vod__c/text()),'-'),City_vod__c/text()),'-'),Address_vod__c/text()))}</Venue>
    {
 let $a:=$i/EM_Event_Team_Member_vod__r/records
 for $y in $a
return
 <User_records>
       <AttendeeLastModifiedDate>{$y/LastModifiedDate/text()}</AttendeeLastModifiedDate>
      <EmployeeName>{$y/Team_Member_vod__r/Name/text()}</EmployeeName>
      <EmployeeID>{$y/Team_Member_vod__r/BIDS_ID_BI__c/text()}</EmployeeID>
  </User_records>
     }
  </records>
  }
  </Response>

上述 XQuery 的实际输出:

{
  "Response": {
    "totalSize": 1,
    "done": true,
    "nextRecordsUrl": "",
    "records": {
      "Product1": "12345",
      "EventLastModifiedDate": "2021-11-10T01:30:55.000+0000",
      "Venue": "UK",
      "User_records": {
        "AttendeeLastModifiedDate": "2021-11-08T02:55:03.000+0000",
        "EmployeeName": "Ish",
        "EmployeeID": "00002113152"
      }
    }
  }
}

预期输出: 输出应为 "records" & "user_records" 的数组格式

{
   "Response":{
      "totalSize":1,
      "done":true,
      "nextRecordsUrl":"",
      "records":[
         {
            "Product1":"12345",
            "EventLastModifiedDate":"2021-11-10T01:30:55.000+0000",
            "Venue":"UK",
            "User_records":[
               {
                  "AttendeeLastModifiedDate":"2021-11-08T02:55:03.000+0000",
                  "EmployeeName":"Ish",
                  "EmployeeID":"00002113152"
               }
            ]
         }
      ]
   }
}

尝试:

<User_records xmlns:json="http://www.json.org" json:array="true">
  <AttendeeLastModifiedDate>{$y/LastModifiedDate/text()}</AttendeeLastModifiedDate>
  <EmployeeName>{$y/Team_Member_vod__r/Name/text()}</EmployeeName>
  <EmployeeID>{$y/Team_Member_vod__r/BIDS_ID_BI__c/text()}</EmployeeID>
</User_records>

我也会为 <records> 做同样的事情。这个例子适用于 eXist-db。 JSON 命名空间在您的环境中可能有所不同。

这是我 运行 在 eXide 中的内容:

xquery version "3.1";
declare option exist:serialize "method=json indent=yes media-type=application/json";

<Response>
  <totalSize>5</totalSize>
  <done>yes</done>
  <nextRecordsUrl>abc</nextRecordsUrl>
  <User_records xmlns:json="http://www.json.org" json:array="true">
      <AttendeeLastModifiedDate>123</AttendeeLastModifiedDate>
      <EmployeeName>456</EmployeeName>
      <EmployeeID>789</EmployeeID>
  </User_records>
</Response>