我有一个看起来像这样的传入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":" Hello world"," 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?
谢谢
答案 0 :(得分:1)
您可以使用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