NiFi Jolt中按nota_fiscal_operacao值设置Coleta字段
解决NiFi Jolt转换中根据字段值动态设置数组对象字段的问题
需求说明
根据输入JSON中nota_fiscal_operacao字段的值("SAIDA"或"ENTRADA"),为输出的Entregas数组中每个对象添加Coleta字段:
- 当
nota_fiscal_operacao为"ENTRADA"时,Coleta设为true - 当
nota_fiscal_operacao为"SAIDA"时,Coleta设为false
输入示例
// 场景1: ENTRADA { "nota_fiscal_operacao": "ENTRADA", "Entregas": [ {"id": 1}, {"id": 2} ] } // 场景2: SAIDA { "nota_fiscal_operacao": "SAIDA", "Entregas": [ {"id": 3}, {"id": 4} ] }
调整后的Jolt Spec
[ { "operation": "modify-overwrite-beta", "spec": { "Entregas": { "*": { "Coleta": "=equals(@(3,nota_fiscal_operacao), 'ENTRADA')" } } } } ]
工作原理
modify-overwrite-beta操作支持用Jolt内置函数修改字段值,且不会删除原有字段Entregas.*表示遍历Entregas数组中的每一个元素对象@(3,nota_fiscal_operacao)通过层级路径获取根节点的nota_fiscal_operacao值(向上跳转3层:当前对象 → Entregas数组 → 根节点)equals函数对比目标值与"ENTRADA",返回对应的布尔值并赋值给Coleta字段
输出示例
// ENTRADA对应的输出 { "nota_fiscal_operacao": "ENTRADA", "Entregas": [ {"id": 1, "Coleta": true}, {"id": 2, "Coleta": true} ] } // SAIDA对应的输出 { "nota_fiscal_operacao": "SAIDA", "Entregas": [ {"id": 3, "Coleta": false}, {"id": 4, "Coleta": false} ] }
内容的提问来源于stack exchange,提问作者Rodrigo Hajime Himauari
相关产品推荐
相关产品推荐

