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

NiFi:如何基于属性匹配提取指定语言的JSON字段值?

解决NiFi中根据属性动态提取JSON多语言字段的问题

由于EvaluateJsonPath不支持直接引用NiFi属性作为JSON路径的动态部分,你可以通过以下两种常用方式实现需求:

方法一:使用ExecuteScript处理器(灵活度高场景推荐)

通过Groovy脚本读取属性、解析JSON并提取目标值,步骤如下:

  1. 添加ExecuteScript处理器,选择Groovy作为脚本语言
  2. 写入如下脚本:
import groovy.json.JsonSlurper

def flowFile = session.get()
if (!flowFile) return

def defaultLang = flowFile.getAttribute('data.defaultLanguage')
def jsonSlurper = new JsonSlurper()
def jsonContent = jsonSlurper.parseText(flowFile.read().getText('UTF-8'))

// 遍历ResourceCategories数组,提取对应语言的名称
def categoryNames = []
jsonContent.ResourceCategories.each { category ->
    if (category.Names.containsKey(defaultLang)) {
        categoryNames.add(category.Names[defaultLang])
    }
}

// 将结果设置为属性,多值用逗号分隔
flowFile.setAttribute('extracted.category.names', categoryNames.join(','))

session.transfer(flowFile, REL_SUCCESS)
  1. 配置处理器输出关系,将处理后的流文件传递到后续组件

方法二:使用JoltTransformJSON + EvaluateJsonPath

先通过Jolt转换把目标语言字段提取到顶层,再用EvaluateJsonPath读取:

  1. 添加JoltTransformJSON处理器,设置Jolt规范为:
[
  {
    "operation": "shift",
    "spec": {
      "ResourceCategories": {
        "*": {
          "Names": {
            "${data.defaultLanguage}": "ResourceCategories[&3].TargetName"
          }
        }
      }
    }
  }
]

该规范会把对应语言的名称提取到每个ResourceCategories元素的TargetName字段中
2. 后续添加EvaluateJsonPath处理器,用表达式$.ResourceCategories[*].TargetName提取值并存储为属性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 06:50:17