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

如何使用NiFi 1.25的JSLTTransformJSON处理器提取数组指定字段为单一条目

NiFi 1.25 JSLTTransformJSON 脚本实现数组字段提取合并

输入JSON

{
  "name": "John Smith",
  "ID": "123123123",
  "address": [
    {
      "location": "123 Main, Citty, ST",
      "info": {
        "type": "single family",
        "own": "yes"
      }
    },
    {
      "location": "123 Lane, Wonderland, ST",
      "info": {
        "type": "apartment",
        "own": "yes"
      }
    },
    {
      "location": "123 Street, Place, ST",
      "info": {
        "type": "condo",
        "own": "no"
      }
    }
  ]
}

需求1:合并所有地址的info.type为residence_type

JSLT脚本

{
  "name": .name,
  "ID": .ID,
  "residence_type": string-join([for (.address) .info.type], ", ")
}

期望输出

{
  "name": "John Smith",
  "ID": "123123123",
  "residence_type": "single family, apartment, condo"
}

脚本说明:通过for遍历address数组,提取每个元素的info.type生成新数组,再用string-join将数组元素以, 为分隔符拼接成字符串。

需求2:合并info.own为"yes"的地址location为residence_type

JSLT脚本

{
  "name": .name,
  "ID": .ID,
  "residence_type": string-join([for (.address) if (.info.own == "yes") .location], "; ")
}

期望输出

{
  "name": "John Smith",
  "ID": "123123123",
  "residence_type": "123 Main, Citty, ST; 123 Lane, Wonderland, ST"
}

脚本说明:遍历address数组时,用if筛选出info.own等于"yes"的元素,提取对应location后,用string-join以; 为分隔符拼接成字符串。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 15:02:41