【问题标题】:Jolt: How to merge in one array elements from different arraysJolt:如何合并来自不同数组的一个数组元素
【发布时间】:2020-12-16 00:25:23
【问题描述】:

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

我的 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”结构中没有预定义数量的元素,所以我试图让它尽可能动态。

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

【问题讨论】:

    标签: arrays json jolt


    【解决方案1】:

    这个规范应该适合你:

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

    输出:

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

    注意事项:

    【讨论】:

    • 如果您accept my answer,我将不胜感激。干杯!
    • 我做到了!我投票了,但没有显示,因为我的声誉低于 15。
    • 谢谢!您也可以单击其下方的复选标记see the picture。接受答案会给你+2分。
    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多