Member since
05-22-2017
7
Posts
0
Kudos Received
0
Solutions
01-12-2023
01:03 PM
In my case I have to fetch a particular key value from a json array. I was trying to do $.[key] but it just splits the array into separate json files. Is there any solution for this ? I wanted the split array as {"test1", "test2"} Jsonarray example: [{"key":"test1", "name":"testName"}, {"key":"test2", "name":"testName2"}]
... View more
05-23-2017
04:06 PM
1 Kudo
Hi @brian adi You can use the SplitJson processor to get the array into individual flowfile records, then use EvaluateJsonPath to pull out the values of interest, and ReplaceText to get the content back into a CSV format. Here is a good example with screenshots showing how to do this using NiFi https://community.hortonworks.com/articles/64069/converting-a-large-json-file-into-csv.html As always, if you find this post useful, don't forget to accept the answer.
... View more
05-24-2017
01:42 PM
1 Kudo
As of NiFi 1.2.0 you may be able to use ConvertRecord to do this, with a JsonTreeReader and a CSVRecordSetWriter (with a Record separator of comma and a value separator of a single space). Prior to 1.2.0 (or if the above approach doesn't work), you can use ExecuteScript. Here is a sample Groovy script that will read all the "term" values from the incoming JSON object and add an attribute called "terms" containing the comma-separated list: def flowFile = session.get()
if(!flowFile) return
def input = session.read(flowFile)
def json = new groovy.json.JsonSlurper().parse(input)
def terms = json.results.collect { it.term }.join(',')
input.close()
flowFile = session.putAttribute(flowFile, 'terms', terms)
session.transfer(flowFile, REL_SUCCESS) If instead you need to replace the content of the flow file with the comma-separated list: def flowFile = session.get()
if(!flowFile) return
flowFile = session.write(flowFile, { inputStream, outputStream ->
def json = new groovy.json.JsonSlurper().parse(inputStream)
def terms = json.results.collect { it.term }.join(',')
outputStream.write(terms.bytes)
} as StreamCallback)
flowFile = session.putAttribute(flowFile, 'mime.type', 'text/csv')
session.transfer(flowFile, REL_SUCCESS)
... View more