You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:基于多级数组的Jolt转换实现分层数据聚合

Jolt多层嵌套聚合实现方案

输入JSON

[
  {
    "classId": 213,
    "className": "Science",
    "classAnimal": "Lion",
    "schoolId": 1,
    "teacherId": 22,
    "teacherName": "John",
    "studentId": "student01",
    "studentName": "Ron"
  },
  {
    "classId": 213,
    "className": "Science",
    "classAnimal": "Lion",
    "schoolId": 1,
    "teacherId": 23,
    "teacherName": "Rina",
    "studentId": "student02",
    "studentName": "Ria"
  },
  {
    "classId": 214,
    "className": "Robotics",
    "classAnimal": "Cow",
    "schoolId": 1,
    "teacherId": 25,
    "teacherName": "Lin",
    "studentId": "student03",
    "studentName": "Roman"
  },
  {
    "classId": 214,
    "className": "Robotics",
    "classAnimal": "Cow",
    "schoolId": 1,
    "teacherId": 27,
    "teacherName": "Albert",
    "studentId": "student02",
    "studentName": "Krish"
  },
  {
    "classId": 214,
    "className": "Robotics",
    "classAnimal": "Cow",
    "schoolId": 1,
    "teacherId": 24,
    "teacherName": "Nova",
    "studentId": "student04",
    "studentName": "Kumar"
  },
  {
    "classId": 218,
    "className": "Arts",
    "classAnimal": "Rabbit",
    "schoolId": 2,
    "teacherId": 33,
    "teacherName": "Mihir",
    "studentId": "student08",
    "studentName": "Laya"
  },
  {
    "classId": 225,
    "className": "Economics",
    "classAnimal": "Horse",
    "schoolId": 2
  }
]

期望输出JSON

[
  {
    "schoolList": [
      {
        "schoolId": 1,
        "classList": [
          {
            "classId": 213,
            "className": "Science",
            "classAnimal": "Lion",
            "teachers": [
              {
                "teacherId": 22,
                "teacherName": "John"
              },
              {
                "teacherId": 23,
                "teacherName": "Rina"
              }
            ],
            "students": {
              "student02": "Ria",
              "student01": "Ron"
            }
          },
          {
            "classId": 214,
            "className": "Robotics",
            "classAnimal": "cow",
            "teachers": [
              {
                "teacherId": 25,
                "teacherName": "Lin"
              },
              {
                "teacherId": 27,
                "teacherName": "Albert"
              },
              {
                "teacherId": 24,
                "teacherName": "Nova"
              }
            ],
            "students": {
              "student02": "Krish",
              "student03": "Roman",
              "student04": "Kumar"
            }
          }
        ]
      },
      {
        "schoolId": 2,
        "classList": [
          {
            "classId": 218,
            "className": "Arts",
            "classAnimal": "Rabbit",
            "teachers": [
              {
                "teacherId": 33,
                "teacherName": "Mihir"
              }
            ],
            "students": {
              "student08": "Laya"
            }
          },
          {
            "classId": 225,
            "className": "Economics",
            "classAnimal": "Horse",
            "teachers": []
          }
        ]
      }
    ]
  }
]

最终Jolt规范

[
  // 第一步:按schoolId和classId分组,归类班级字段、教师、学生信息
  {
    "operation": "shift",
    "spec": {
      "*": {
        "schoolId": "schools.&1.schoolId",
        "classId": "schools.&1.classes.&2.classId",
        "className": "schools.&1.classes.&2.className",
        "classAnimal": "schools.&1.classes.&2.classAnimal",
        "teacherId": "schools.&1.classes.&2.teachers[#2].teacherId",
        "teacherName": "schools.&1.classes.&2.teachers[#2].teacherName",
        "studentId": "schools.&1.classes.&2.students.@(1,studentId)",
        "studentName": "schools.&1.classes.&2.students.@(1,studentId)"
      }
    }
  },
  // 第二步:去重教师数组,整理学生键值对结构
  {
    "operation": "shift",
    "spec": {
      "schools": {
        "*": {
          "schoolId": "schools.[&1].schoolId",
          "classes": {
            "*": {
              "classId": "schools.[&2].classes.[&1].classId",
              "className": "schools.[&2].classes.[&1].className",
              "classAnimal": "schools.[&2].classes.[&1].classAnimal",
              "teachers": {
                "*": {
                  "@": "schools.[&3].classes.[&2].teachers[]"
                }
              },
              "students": {
                "*": {
                  "@": "schools.[&3].classes.[&2].students.&1"
                }
              }
            }
          }
        }
      }
    }
  },
  // 第三步:给无教师的班级添加空teachers数组
  {
    "operation": "default",
    "spec": {
      "schools": {
        "*": {
          "classes": {
            "*": {
              "teachers": []
            }
          }
        }
      }
    }
  },
  // 第四步:调整为schoolList -> classList的嵌套结构
  {
    "operation": "shift",
    "spec": {
      "schools": {
        "*": {
          "schoolId": "schoolList[&1].schoolId",
          "classes": {
            "*": {
              "@": "schoolList[&2].classList[&1]"
            }
          }
        }
      }
    }
  },
  // 第五步:将classAnimal转换为小写(匹配期望输出)
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "schoolList": {
        "*": {
          "classList": {
            "*": {
              "classAnimal": "=toLower"
            }
          }
        }
      }
    }
  },
  // 第六步:将结果包裹在数组中
  {
    "operation": "shift",
    "spec": {
      "@": "[]"
    }
  }
]

转换逻辑说明

  1. 第一层Shift:把原始数据按schoolId和classId做初步分组,将教师信息临时存入数组,学生信息按studentId作为键暂存对应名称。
  2. 第二层Shift:对分组后的教师数组做去重合并(通过teachers[]自动收集唯一的教师对象),同时确认学生的键值对结构。
  3. Default操作:处理没有教师的班级(比如Economics班),默认添加空的teachers数组,避免字段缺失。
  4. 第三层Shift:将临时的schools结构转换为期望的schoolList和classList嵌套层级。
  5. Modify操作:将classAnimal字段转换为小写,完全匹配期望输出的格式。
  6. 最后一层Shift:将最终对象包裹到数组中,与期望输出的外层结构一致。

内容的提问来源于stack exchange,提问作者Abhinav Prakash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 01:15:52