【问题标题】:Jolt- Based on the index need to iterate a list and form common object from the indexJolt-基于索引需要迭代一个列表并从索引中形成公共对象
【发布时间】:2022-01-22 18:28:04
【问题描述】:

我需要迭代一个可以具有相同标签名称但具有不同索引(如 0、1、2)的列表。所以我需要迭代列表,取出公共索引并从该索引中取出名称和值标签并形成另一个列表。

请求:

{
  "characteristic": [
    {
      "name": "BucketName",
      "value": "testName0",
      "@type": "bucketInfo",
      "arrayIndex": "0"
    },
    {
      "name": "BucketName",
      "value": "testName1",
      "@type": "bucketInfo",
      "arrayIndex": "1"
    },
    {
      "name": "BucketName",
      "value": "testName2",
      "@type": "bucketInfo",
      "arrayIndex": "2"
    },
    {
      "name": "BucketId",
      "value": "testId0",
      "@type": "bucketInfo",
      "arrayIndex": "0"
    },
    {
      "name": "BucketId",
      "value": "testId1",
      "@type": "bucketInfo",
      "arrayIndex": "1"
    },
    {
      "name": "BucketId",
      "value": "testId2",
      "@type": "bucketInfo",
      "arrayIndex": "2"
    }
  ]
}

需要回复:

{
  "bucketList": [
    {
      "BucketName": "testName0",
      "BucketId": "testId0"
    },
    {
      "BucketName": "testName1",
      "BucketId": "testId1"
    },
    {
      "BucketName": "testName2",
      "BucketId": "testId2"
    }
  ]
}

我们如何根据来自 alist 的索引来实现这一点?

当有更多元素时,如何处理这种情况以跳过如果值未到来并仅添加即将到来的标签。 示例请求:

{
  "characteristic": [
    {
      "name": "BucketName",
      "value": "testName0",
      "@type": "bucketInfo",
      "arrayIndex": "0"
    },
    {
      "name": "BucketId",
      "value": "testId0",
      "@type": "bucketInfo",
      "arrayIndex": "0"
    },
    {
      "name": "BucketType",
      "value": "testType1",
      "@type": "bucketInfo",
      "arrayIndex": "1"
    },
    {
      "name": "BucketId",
      "value": "testId1",
      "@type": "bucketInfo",
      "arrayIndex": "1"
    },
    {
      "name": "BucketName",
      "value": "testName2",
      "@type": "bucketInfo",
      "arrayIndex": "2"
    },
    {
      "name": "BucketId",
      "value": "testId2",
      "@type": "bucketInfo",
      "arrayIndex": "2"
    },
    {
      "name": "BucketId",
      "value": "testId3",
      "@type": "bucketInfo",
      "arrayIndex": "3"
    },
    {
      "name": "BucketName",
      "value": "testName3",
      "@type": "bucketInfo",
      "arrayIndex": "3"
    },
    {
      "name": "BucketData",
      "value": "testData3",
      "@type": "bucketInfo",
      "arrayIndex": "3"
    }
  ]
}

预期响应:

{
    "bucketlist": [
        {
            "BucketName": "testName0",
            "BucketId": "testId0"
        },
        {
            "BucketType": "testType1",
            "BucketId": "testId1"
        },
        {
            "BucketName": "testName2",
            "BucketId": "testId2"
        },
        {
            "BucketName": "testName3",
            "BucketId": "testId3",
            "BucketData": "testData3"
        }
    ]
}

【问题讨论】:

    标签: java json jolt


    【解决方案1】:

    您可以应用两个连续的 shift 转换。以 "@(2,arrayIndex)" 为公因子,以便在第一次转换中组合常用标记数组下的元素,然后在第二次转换中根据需要显示它们,例如 p>

    [
      {
        "operation": "shift",
        "spec": {
          "characteristic": {
            "*": {
              "value": {
                "@(1,value)": "@(2,arrayIndex).@(2,name)"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "bucketList[]"
        }
      }
    ]
    

    【讨论】:

    • 感谢您的快速帮助。但是当有更多这样的元素并且跳过一些元素时,不会添加新元素的值。总是 bucketId 和 bucketName 来自上述规范。
    • 我在@Barbaros 上面编辑了问题
    • 是的,需要动态@Barbaros
    • 感谢@Barbaros 的快速解决方案
    • 不客气@Chethan,好好学习
    猜你喜欢
    • 2017-06-07
    • 1970-01-01
    • 2021-10-31
    • 2021-04-23
    • 2019-05-25
    • 2016-05-16
    • 2018-09-21
    • 2020-03-01
    • 1970-01-01
    相关资源
    最近更新 更多