首页 > 解决方案 > Jolt:如何合并来自不同数组的一个数组元素

问题描述

我正在尝试合并数组中不同元素的内容。

我的json看起来像这样:

{
  "Messages": {
    "test": "test1",
    "CreditCheck": [
      {
        "name": "Credit Score",
        "score": 15,
        "percentage": 20
      },
      {
        "name": "Cards Score",
        "score": 15,
        "percentage": 20
      },
      {
        "name": "Bank Score",
        "score": 20,
        "percentage": 20
      }
    ]
  }
}

我目前的 Jolt Spec 是这样的:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "test": "myTesting",
        "CreditCheck": {
          "*": {
            "name": "creditName.[]",
            "score": "creditScore.[]"
          }
        }
      }
    }
  }
]

我得到以下信息:

{
  "myTesting" : "test1",
  "creditName" : [ "Credit Score", "Cards Score", "Bank Score" ],
  "creditScore" : [ 15, 15, 20 ]
}

但我需要实现的是以下几点:

{
  "myTesting" : "test1",
  "creditNames&Scores" : [ "Credit Score": 15, "Cards Score": 15, "Bank Score": 20 ],
}

“CreditCheck”结构中没有预定义数量的元素,所以我试图让它尽可能动态。

提前感谢您的帮助,最好的!

标签: arraysjsonjolt

解决方案


该规范应该适合您:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "test": "myTesting",
        "CreditCheck": {
          "*": {
            "score": "creditNames\\&Scores[].@(1,name)"
          }
        }
      }
    }
  }
]

输出:

{
  "myTesting" : "test1",
  "creditNames&Scores" : [ { "Credit Score" : 15 }, { "Cards Score" : 15 }, {   "Bank Score" : 20} ]
}

笔记:


推荐阅读