Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

How to transform a json array to a string list in NIFI

avatar

Hello,

i have a queryCassandra which generate a json like this one:

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

Now, i want to get from this all the terms 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 ACCEPTED SOLUTION

avatar
Master Guru

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 solution in original post

1 REPLY 1

avatar
Master Guru

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)