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

JOLT实现NiFi中JSON子数组迁移至父级并生成带序号独立对象

NiFi JOLT 转换Spec实现方案

直接给出可用的JOLT spec如下,使用shift操作完成字段迁移、重命名、过滤需求:

[
  {
    "operation": "shift",
    "spec": {
      // 保留原核心字段,可根据实际输入的key调整左右侧字段名
      "id": "id",
      "number": "number",
      "title": "title",
      "user": "creator_info",
      "comments": "comment_count",
      "created_at": "created_at",
      "updated_at": "updated_at",
      "closed_at": "closed_at",
      "body": "issue_content",
      // 处理labels数组
      "labels": {
        "*": {
          "id": "label_&1.id",
          "name": "label_&1.name",
          "description": "label_&1.description"
        }
      },
      // 处理assignees数组,同时按需求重命名字段
      "assignees": {
        "*": {
          "login": "user_&1.assignee_login",
          "id": "user_&1.assignee_id",
          "site_admin": "user_&1.is_assignee_site_admin"
        }
      }
    }
  }
]
  • 核心字段保留逻辑:上述spec已覆盖你提到的所有核心字段需求,若输入JSON中对应字段的key和示例不一致,直接修改冒号左侧的原字段名即可。
  • labels数组处理逻辑:用通配符*匹配数组所有元素,&1取当前元素在数组中的索引(从0开始计数),自动拼接为label_0、label_1格式的父级字段,同时自动过滤id、name、description之外的冗余字段。
  • assignees数组处理逻辑:同样用索引拼接user_0、user_1格式的父级字段,直接在冒号右侧定义重命名后的key,适配你的字段修改需求;若assignees为空数组,转换后不会生成对应user_*字段,你可直接用现有NiFi流补充null值即可。

输入输出验证示例

输入示例

{
  "id": 12345,
  "number": 233,
  "title": "修复登录异常问题",
  "user": {"login": "creator001", "id": 111},
  "comments": 5,
  "created_at": "2024-01-01T12:00:00Z",
  "body": "登录时提示参数错误",
  "labels": [
    {"id": 1, "name": "bug", "description": "功能缺陷", "color": "red"},
    {"id": 2, "name": "high priority", "description": "高优先级", "color": "yellow"}
  ],
  "assignees": [
    {"login": "dev001", "id": 222, "site_admin": false, "email": "dev001@test.com"},
    {"login": "dev002", "id": 333, "site_admin": true, "email": "dev002@test.com"}
  ]
}

输出示例

{
  "id": 12345,
  "number": 233,
  "title": "修复登录异常问题",
  "creator_info": {"login": "creator001", "id": 111},
  "comment_count": 5,
  "created_at": "2024-01-01T12:00:00Z",
  "issue_content": "登录时提示参数错误",
  "label_0": {"id": 1, "name": "bug", "description": "功能缺陷"},
  "label_1": {"id": 2, "name": "high priority", "description": "高优先级"},
  "user_0": {"assignee_login": "dev001", "assignee_id": 222, "is_assignee_site_admin": false},
  "user_1": {"assignee_login": "dev002", "assignee_id": 333, "is_assignee_site_admin": true}
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 01:18:04