使用 Jolt 变换对 JSON 个元素进行分组

Grouping JSON elements using Jolt transform

我需要 jolt 变换规范方面的帮助。以下是我目前的工作。

输入:

[
  {
    "ID": "1234",
    "Date": "2020-12-10",
    "Time": "06:00:00",
    "Rate": null,
    "Interest": null,
    "Term": 99
  },
  {
    "ID": "1234",
    "Date": "2020-12-11",
    "Time": "07:00:00",
    "Rate": 8,
    "Interest": null,
    "Term": 99
  }
]

使用的颠簸代码:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "ID": "@(1,ID).id",
        "Date": "@(1,ID).date",
        "Time": "@(1,ID).group.time",
        "Rate": "@(1,ID).group.rate",
        "Interest": "@(1,ID).group.interest",
        "Term": "@(1,ID).group.term"
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "id": "ONE"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

当前输出:

[
  {
    "id": "1234",
    "date": ["2020-12-10", "2020-12-11"],
    "group": {
        "time": ["06:00:00", "07:00:00"],
        "rate": 8,
        "interest": null,
        "term": [99, 99]
    }
 }
]

预期输出

[
  {
    "id": "1234",
    "date": "2020-12-10",
    "group": {
      "time": "06:00:00",
      "rate": null,
      "interest": null,
      "term": 99
    }
  },
  {
    "id": "1234",
    "date": "2020-12-11",
    "group": {
      "time": "07:00:00",
      "rate": 8,
      "interest": null,
      "term": 99
    }
  }
]

仅使用单个 json 对象时,此代码工作正常。但是当我们使用具有相同 ID 的多个项目时,它会开始对所有相关字段进行分组。

您可以使用方括号表示法 ([&1]) 作为公因子,同时将除 idDate 之外的其余元素限定为 group 比如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "ID": "[&1].&",
        "Date": "[&1].&",
        "*": "[&1].group.&"
      }
    }
  }
]