NiFi - Convert comma delimited string in json to array

Viewed 778

I'm looking for a way to convert a comma delimited string in a json to an array in NiFi.

Then length of the array is variable. I've tried a few things now, including using the jolt transformer, but the version of jolt with nifi doesn't support split.

My flow file looks like:

{
  "arrStr": "abc,def,hij",
  "something": "else"
}

And I'm trying to convert it to:

{
  "arrStr": ["abc", "def", "hij"],
  "something": "else"
}

Update 7/10:

Sorry, should have included that I am on nifi version 1.7.1 which I think has jolt version 0.1.0.

2 Answers

Shout out to @daggett for commenting about groovy. I went with that route and it's real easy to do. I had to use ExecuteScript processor, not the ExecuteGroovy processor, as the ExecuteGroovy processor didn't load up the IOUtils I wanted to use.

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import org.apache.commons.io.IOUtils

import java.nio.charset.StandardCharsets

def flowFile = session.get();
if (flowFile == null) {
    return;
}

flowFile = session.write(flowFile,
        { inputStream, outputStream ->
            def content = IOUtils.toString(inputStream, java.nio.charset.StandardCharsets.UTF_8)

            def slurped = new JsonSlurper().parseText(content)
            def builder = new JsonBuilder(slurped)
            
            builder.content.arrStr = builder.content.arrStr.split(',')
            
            outputStream.write(builder.toPrettyString().getBytes(StandardCharsets.UTF_8))
        } as StreamCallback)
        
session.transfer(flowFile, ExecuteScript.REL_SUCCESS)

Related