首页 > 解决方案 > 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 添加到实际对象中。请帮助解决此问题。

标签: jsonjolt

解决方案


您似乎只需要将所有内容收集到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": {
      "*": {
        "*": "[]"
      }
    }
  }
]

推荐阅读