Nifi:如何在JSON中编写子/嵌套元素

Nifi:如何在JSON中编写子/嵌套元素,json,apache-nifi,Json,Apache Nifi,我有一个传入的Nifi流文件,看起来像这样 {"filename":"ok.txt.2018-01-27-16-18-03-290","test":"{\"filename\":ok.txt,\"test\":23}","timestamp":"Sat Jan 27 16:18:03 UTC 2018"} 我想向测试元素添加一个子/嵌套元素test:{text:helloworld,Country:Espana}。换句话说,我希望我的JSON输出看起来像 {"filename":"ok.txt

我有一个传入的Nifi流文件,看起来像这样

{"filename":"ok.txt.2018-01-27-16-18-03-290","test":"{\"filename\":ok.txt,\"test\":23}","timestamp":"Sat Jan 27 16:18:03 UTC 2018"}
我想向测试元素添加一个子/嵌套元素test:{text:helloworld,Country:Espana}。换句话说,我希望我的JSON输出看起来像

{"filename":"ok.txt.2018-01-27-16-18-03-290","test":"{\"filename\":ok.txt,\"test\":23, "text": {"Hello world","Country":"Espana"}}","timestamp":"Sat Jan 27 16:18:03 UTC 2018"}
可以在Nifi中将子元素添加到JSON中吗

谢谢

您可以将ExecuteGroovyScript 1.5.0处理器与以下代码一起使用

import groovy.json.*

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

ff.write{rawIn,rawOut->
    //parse json from input stream of the flow file
    def json = rawIn.withReader("UTF-8"){reader-> 
        new JsonSlurper().parse( reader )
    }
    //modify json 
    json."test" = [
            "text"     :"Hello world",
            "Country"  :"Espana"
        ]
    //write json to flow file output stream
    rawOut.withWriter("UTF-8"){writer->
        new JsonBuilder(json).writeTo(writer)
    }
}

REL_SUCCESS << ff