JOLT - 嵌套数组总和

JOLT - Nested arrays sum

我找不到此转换的解决方案。我的目标是对所有部分求和并将其存储到长度与 operations.

相同的数组中

输入数据:

{
  "operations": [
    {
      "partOne": 10,
      "partTwo": 12.5,
      "partThree": 30.5
    },
    {
      "partOne": 25.5,
      "partTwo": 2,
      "partThree": 7.5
    }
  ]
}

输出数据:

{
  "costs": [
    53, // operations[0].partOne + operations[0].partTwo + operations[0].partThree
    35 // operations[1].partOne + operations[1].partTwo + operations[1].partThree
  ]
}

编辑:我找到了这个中间步骤,仍然缺少求和部分。

[
  {
    "operation": "shift",
    "spec": {
      "operations": {
        "*": {
          "partOne": "costs[&1].[]",
          "partTwo": "costs[&1].[]",
          "partThree": "costs[&1].[]"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "costs": "=doubleSum(@(1,costs[&1]))"
      }
    }
  }
]

此外,如果您能分享一些关于 JOLT 规范的文档,我将不胜感激。找不到任何让我正确开始的东西。

您可以像这样去掉方括号,在当前操作之间添加一个移位操作作为重新排列:

[
  {
    "operation": "shift",
    "spec": {
      "operations": {
        "*": {
          "partOne": "costs&1",
          "partTwo": "costs&1",
          "partThree": "costs&1"
        }
      }
    }
      },
  {
    "operation": "shift",
    "spec": {
      "*": "&"
    }
      },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "tot_costs0": "=doubleSum(@(1,costs0))",
      "tot_costs1": "=doubleSum(@(1,costs1))"
    }
  }
]

P.S。 this source 可能会有帮助。

我找到了解决方案。

规格:

[
  {
    "operation": "shift",
    "spec": {
      "operations": {
        "*": {
          "partOne": "costs[&1].[]",
          "partTwo": "costs[&1].[]",
          "partThree": "costs[&1].[]"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "costs": {
        "*": "=doubleSum"
      }
    }
  }
]