json - Jolt Conversion - 在列表中迭代列表并形成单个列表
问题描述
我正在尝试在列表中迭代列表并形成具有多个对象的单个列表。迭代列表,我能够实现。但是,如果单个列表中有更多对象,则不会在列表迭代到每个对象之前应用标签。
我的输入请求如下:
[
{
"success": [
{
"id": "4",
"Offers": [
{
"name": "Optional",
"type": {
"id": "1",
"name": "Optional"
},
"productOfferings": [
{
"id": "3",
"name": "Test1"
}
]
},
{
"name": "Default",
"type": {
"id": "2",
"name": "Default"
},
"productOfferings": [
{
"id": "1",
"name": "Test2"
},
{
"id": "2",
"name": "Test3"
}
]
}
]
}
]
}
]
我的规格如下:
[
{
"operation": "shift",
"spec": {
"*": {
"success": {
"*": {
"Offers": {
"*": {
"name": "[&1].[&3].typeName",
"type": {
"id": "[&2].[&4].typeNameId",
"name": "[&2].[&4].typeNameValue"
},
"productOfferings": {
"*": {
"id": "[&3].[&1].id",
"name": "[&3].[&1].name"
}
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[]"
}
}
}
]
从规范接收的输出:
[
{
"typeName": "Optional",
"typeNameId": "1",
"typeNameValue": "Optional",
"id": "3",
"name": "Test1"
},
{
"typeName": "Default",
"typeNameId": "2",
"typeNameValue": "Default",
"id": "1",
"name": "Test2"
},
{
"id": "2",
"name": "Test3"
}
]
但预期输出如下:
[
{
"typeName": "Optional",
"typeNameId": "1",
"typeNameValue": "Optional",
"id": "3",
"name": "Test1"
},
{
"typeName": "Default",
"typeNameId": "2",
"typeNameValue": "Default",
"id": "1",
"name": "Test2"
},
{
"typeName": "Default",
"typeNameId": "2",
"typeNameValue": "Default",
"id": "2",
"name": "Test3"
}
]
如果 productOfferings 对象中有更多对象,我无法将 typeName、typeNameId、typeNameValue 添加到实际对象中。请帮助解决此问题。
解决方案
您似乎只需要将所有内容收集到productOfferings数组中,同时在每个键前面加上公共标识符[&3].[&1]
,例如
[
{
"operation": "shift",
"spec": {
"*": {
"success": {
"*": {
"Offers": {
"*": {
"productOfferings": {
"*": {
"@(2,name)": "[&3].[&1].typeName",
"@(2,type.id)": "[&3].[&1].typeNameId",
"@(2,type.name)": "[&3].[&1].typeNameValue",
"*": "[&3].[&1].&"
}
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[]"
}
}
}
]
推荐阅读
- laravel - Laravel 删除一条记录的路由
- python - 如何通过随机初始化权重从头开始训练 keras.applications 中给出的模型?
- c++ - 为什么 include 语句返回一个错误,基本上表明该语言 (C++) 存在错误?
- python - 在python datetime中,为什么第一个月'January' = 1,但'Monday',一周的第一天= 0?
- sql - 我无法让它按月显示信息。是 GROUP BY 还是我的 LOOP 搞砸了?
- python-3.x - python MVC,如何让函数不按按钮自动运行?
- javascript - 如何从 Twilio 函数将数据加载到 S3 存储桶中?
- java - 匕首/缺少绑定。没有@Provides-annotated 方法就不能提供输出器
- laravel - 身份验证登录不适用于 laravel 6 中的新用户
- sql - 如何在 SQL Server 中使用 CTE 显示每个销售代表每年的销售订单总数?