使用 Jolt 对 Json 数组进行分组

Grouping Json array with Jolt

我很难理解如何使用 Jolt 对 JSON 数组进行分组。

我有这样的输入

{
  "staff": "David",
  "staffId": "D12345",
  "office": "office A",
  "teachClasses":[
    {
      "className": "Intro to Json",
      "classRoom": "room A",
      "day": "Sunday"
    },
    {
      "className": "Intro to Jolt",
      "classRoom": "room C",
      "day": "Saturday"
    },
    {
      "className": "Intro to Json",
      "classRoom": "room B",
      "day": "Friday"
    },
    {
      "className": "Intro to Javascript",
      "classRoom": "room D",
      "day": "Sunday"
    },
    {
      "className": "Intro to Jolt",
      "classRoom": "room E",
      "day": "Wednesday"
    }
  ]
}

我想按“teachClasses.className”分组为数组中的单个对象。

预期输出 :

[
  {
    "staff": "David",
    "staffId": "D12345",
    "office": "office A",
    "className": "Intro to Json",
    "teachClasses": [
      {
        "className": "Intro to Json",
        "classRoom": "room A",
        "day": "Sunday"
      },
      {
        "className": "Intro to Json",
        "classRoom": "room B",
        "day": "Friday"
      }
    ]
  },
  {
    "staff": "David",
    "staffId": "D12345",
    "office": "office A",
    "className": "Intro to Jolt",
    "teachClasses": [
      {
        "className": "Intro to Jolt",
        "classRoom": "room C",
        "day": "Saturday"
      },
      {
        "className": "Intro to Jolt",
        "classRoom": "room E",
        "day": "Wednesday"
      }
    ]
  },
  {
    "staff": "David",
    "staffId": "D12345",
    "office": "office A",
    "className": "Intro to Javascript",
    "teachClasses": [
      {
        "className": "Intro to Javascript",
        "classRoom": "room D",
        "day": "Sunday"
      }
    ]
  }
]

有办法吗? 你能展示一个样本规格并做一些解释吗? 另外,我正在寻找任何学习 Jolt 的教程。有什么建议吗?

你可以使用@(0,className)作为公共值来改造公共键下的对象,然后trim第二个内的键名spec,然后使用 cardinality spec 选择最左边的元素,例如

[
  {
    "operation": "shift",
    "spec": {
      "teachClasses": {
        "*": {
          "@(2,staff)": "@(0,className).staff",
          "@(2,staffId)": "@(0,className).staffId",
          "@(2,office)": "@(0,className).office",
          "@": "@(0,className).&2[]"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "staff": "ONE",
        "staffId": "ONE",
        "office": "ONE"
      }
    }
  }
]