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

NiFi使用JoltTransformJSON转换嵌套JSON为CSV输出异常如何解决

问题根因

你当前使用的Jolt规则中仅用rc数组的内部索引&1作为输出数组的索引,导致不同顶层对象下相同索引的rc条目被合并到同一个输出对象中,所有字段都变成多值数组,无法被ConvertRecord正常解析。

修正后的Jolt转换规则
[
  {
    "operation": "shift",
    "spec": {
      "*": {
        // 遍历rc数组中的每个元素
        "rc": {
          "*": {
            // 取上层的city、phone字段,使用【顶层数组索引+rc内部索引】作为输出的唯一索引,避免合并
            "@(2,city)": "[&3][&1].city",
            "@(2,phone)": "[&3][&1].phone",
            // 复制rc内部的所有字段
            "*": "[&3][&1].&"
          }
        },
        // 处理rc为空的场景(比如London那条记录),单独生成一条行数据
        "rc": {
          "0": null
        },
        "@(city)": "[&1].city",
        "@(phone)": "[&1].phone"
      }
    }
  },
  // 可选:过滤不需要输出的字段,只保留你需要的city、lastName、title
  {
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "phone": "",
          "email": "",
          "firstName": ""
        },
        "phone": ""
      }
    }
  },
  // 把二维数组拍平为一维数组,适配ConvertRecord的输入要求
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[]"
      }
    }
  }
]
后续处理器配置说明
  • JoltTransformJSON输出的是扁平化的单级JSON数组,每个元素对应CSV的一行,直接接入ConvertRecord即可
  • 若需要严格匹配你给出的CSV字段顺序,在CSVRecordSetWriter中配置Schema Text如下即可:
{
  "type": "record",
  "name": "exportSchema",
  "fields": [
    {"name": "city", "type": ["string", "null"]},
    {"name": "lastName", "type": ["string", "null"]},
    {"name": "title", "type": ["string", "null"]}
  ]
}
  • 注意你给出的CSV示例中Oklahome为拼写错误,转换后输出的正确值为Oklahoma。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 02:06:03