2

我从 API 以数组格式(用于多条记录)和以对象格式获取单条记录的 JSON 输出。相反,消费者希望保持一种格式 - 作为单个记录的数组。请让我知道是否有办法以数组格式显示 JSON 输出,而不管使用 XQuery 的单条/多条记录

我尝试了以下 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"
               }
            ]
         }
      ]
   }
}

4

1 回答 1

2

尝试:

<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>
于 2021-11-11T13:49:57.633 回答