首页 > 解决方案 > 在 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"
          }
        ]
      }
    ]
  }
}

标签: jolt

解决方案


您可以使用此班次规范

[
  {
    "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" }
        }
      }
    }
  }
]

在此处输入图像描述


推荐阅读