如何使用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
相关产品推荐
相关产品推荐

