groovyapache-nifi

Is there the opposite of AttributesToJSON in NiFi?


I am looking for a way to get the attributes back from a JSON file. Basically, the inverse of the existing AttributesToJSON processor.

I'm pretty new to NiFi, and thought this would be something out of the box. However, it looks like this requires a custom processor or script. I am running NiFi 2.0, so I could use Clojure or Groovy scripts with the ExecuteScript processor. It looks like python is now only supported by creating your own processors.

Does anyone know how I could do this?

Thanks!


Solution

  • As you said, you can use Groovy in ExecuteScript for this, here's a script that does it:

    import org.apache.commons.io.IOUtils
    import java.nio.charset.*
    def flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    def slurper = new groovy.json.JsonSlurper()
    def attrs = [:] as Map<String,String>
    session.read(flowFile,
        { inputStream ->
            def text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
            def obj = slurper.parseText(text)
            obj.each {k,v ->
               attrs[k] = v.toString()
            }
        } as InputStreamCallback)
    flowFile = session.putAllAttributes(flowFile, attrs)
    session.transfer(flowFile, REL_SUCCESS)