jolt - 在 Jolt 中将多个字符串列表转换为不同的对象
问题描述
我的请求数组中有多个字符串列表。我必须迭代每个列表并获取它的第一个元素并形成一个对象作为 bucketList 类似地每个列表的第二个元素并形成另一个对象。这怎么能在颠簸中实现?
请求包:
{
"recordSet": [
{
"id": "123",
"bucketIdArray": [ "M03", "M02" ],
"bucketNameArray": [ "EmployeeBucket200", "EmployeeBucket100" ],
"bucketUsageArray": [ "500000000", "5000" ],
"postBucketValueArray": [ "5004000", "5000" ]
},
{
"id": "456",
"bucketIdArray": [ "M04" ],
"bucketNameArray": [ "EmployeeBucket300" ],
"bucketUsageArray": [ "500000000" ],
"postBucketValueArray": [ "5004000" ]
}
]
}
预期响应:
{
"datas": {
"historyDetails": [
{
"historyId": "123",
"bucketlist": [
{
"bucketId": "M03",
"bucketName": "EmployeeBucket200",
"bucketUsage": "500000000",
"postBucketValue": "5004000"
},
{
"bucketId": "M02",
"bucketName": "EmployeeBucket300",
"bucketUsage": "50000",
"postBucketValue": "5004000"
}
]
},
{
"historyId": "456",
"bucketlist": [
{
"bucketId": "M04",
"bucketName": "EmployeeBucket300",
"bucketUsage": "500000000",
"postBucketValue": "5004000"
}
]
}
]
}
}
解决方案
您可以使用此班次规范
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"id": "datas.historyDetails[&1].&",
"*": { "*": "datas.historyDetails[&2].bucketlist[&].&1" }
}
}
}
}
]
如果Array
需要从键名中删除字符串,则应为它们显式指定每个键值对,例如
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "datas.historyDetails[&1].&",
"bucketIdArray": { "*": "datas.historyDetails[&2].bucketlist[&].bucketId" },
"bucketNameArray": { "*": "datas.historyDetails[&2].bucketlist[&].bucketName" },
"bucketUsageArray": { "*": "datas.historyDetails[&2].bucketlist[&].bucketUsage" },
"postBucketValueArray": { "*": "datas.historyDetails[&2].bucketlist[&].postBucketValue" }
}
}
}
}
]
另一种选择是在我们原来的班次规范之前增加三个步骤,以防止每个键单独使用Array
后缀进行硬编码,例如
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"$": "&2.&.key",
"@": "&2.&.val"
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"key": "=split('Array',@(1,&))"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"val": "&.[&2].@(1,key[0])"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"id": "datas.historyDetails[&1].&",
"*": { "*": "datas.historyDetails[&2].bucketlist[&].&1" }
}
}
}
}
]
推荐阅读
- java - 具有邻接表java的图形实现
- python - 使用 BeautifulSoup 查找 Javascript 变量定义
- java - 方法(循环)中的扫描仪不接受大于/小于符号
- javascript - 如何按 id 加载面板?
- c# - 为多个类似的类创建一个通用方法
- c - 用C画三角形和正方形
- javascript - 在 FullCalendar 中将时区设置为德国
- firebase - 在 Firestore 中,安全规则是否会在集合快照更新之前生效?
- powershell - 键入“exit”会关闭 foo.exe 和 powershell
- python - 如果用户是匿名的,装饰器会更改视图功能逻辑