Archives of Support Questions (Read Only)

This is an archived board for historical reference. Information and links may no longer be available or relevant
Announcements
This board is archived and read-only for historical reference. To ask a new question, please post a new topic on the appropriate active board.

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

avatar
New Member

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

2 REPLIES 2

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)

avatar
Explorer

Thanks mburgess! This was helpful.