NIFI: get value from json

久未见 提交于 2020-06-28 06:28:11

问题


i have a queryCassandra which generate json like this one:

{"results":[{"term":"term1"},{"term":"term2"}..]}

Now, i want to get from this all the term values separated by some separator in string format; ex : term1,term2,term3

So i can pass this list as a string parameter for a java main program which i've alreat set.

(i only need the transofrmation, not the java program execution)

Thank you !


回答1:


You can easily get those values by using following ways.

GetFile-->EvaluateJsonPath-->PutFile

In get file you have to specify location of json file.

In EvaluateJsonPath configure like following properties.,

Destination:flowfile-attribute
Return Type:json
input.term1:$.results.[0].term           //To get term
input.term2:$.results.[1].term

At the result of Evaluate json you have two attributes in which having those values.

Result attributes:

input.term1: term1

input.term2: term2

Above code works for me,so feel free to upvote/accept as answer.




回答2:


as a variant use ExecuteScript with groovy lang:

import groovy.json.*

//get input file
def flowFile = session.get()
if(!flowFile)return

//parse json to map/array objects
def content = session.read(flowFile).withStream{ stream-> return new JsonSlurper().parse( stream ) }

//transform
content = content.results.collect{ it.term }.join(',')

//write new content
flowFile = session.write(flowFile,{ stream->
    stream << content.getBytes("UTF-8")
} as OutputStreamCallback)

session.transfer(flowFile, REL_SUCCESS)


来源:https://stackoverflow.com/questions/44112731/nifi-get-value-from-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!