0

I have a Nifi Groovy Script. Assigns fields to nifi attributes but it define null if json values ​​are empty. if json value is null I want to define attribute empty.

null attributes look like this ;

enter image description here

I want to like this ;

enter image description here

this is the script i use ;

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)

2 Answers 2

0
flowFile = session.putAllAttributes(flowFile, attrs.collectEntries{k,v->[k,v?:'']})
Sign up to request clarification or add additional context in comments.

1 Comment

i got an error ; ExecuteGroovyScript[id=01841011-21f7-17f3-3f20-e267910070a8] groovy.lang.MissingMethodException: No signature of method: org.apache.nifi.processors.groovyx.flow.GroovyProcessSessionWrap.putAllAttributes() is applicable for argument types: (org.apache.nifi.processors.groovyx.flow.GroovySessionFile, ArrayList) values: [WRAP[StandardFlowFileRecord[uuid=0d722ced-0e23-4f0f-a8b4-0026fbadcb6c,claim=StandardContentClaim
0

This is i solved 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 ->

          if (v.toString()=="null")
          {
            attrs[k] = ''
          } else {
            attrs[k] = v.toString()
          }

        }
    } as InputStreamCallback)
flowFile = session.putAllAttributes(flowFile, attrs)
session.transfer(flowFile, REL_SUCCESS)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.