xquery - 使用 XQuery 以对象格式获取单个记录而不是数组的 JSON 输出
问题描述
我从 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"
}
]
}
]
}
}
解决方案
尝试:
<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>
推荐阅读
- symfony - 如何在 Symfony 4 中防止用户自动注销?
- python - 熊猫数据框列的 KeyError 异常
- c# - EF Core > 删除实体(软删除)> 实体状态保持不变
- raspberry-pi - Raspberry pi 4 Model B 更改日期格式
- azure-active-directory - 如何将 Azure AD B2C 连接到 Azure AD
- rust - 如何更新 HashMap 中的键?
- sqlite - SQlite 大数用逗号分隔
- android - 在 Android 中构建和使用 Oboe Resampler
- javascript - 为什么刷新后本地存储值清零?
- c - 为什么这个自动展开列表的尾部无法成功显示?