[ 
https://issues.apache.org/jira/browse/NIFI-11197?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17788640#comment-17788640
 ] 

macdoor615 commented on NIFI-11197:
-----------------------------------

[~gbc] Here is my groovy script 

 
{code:java}
import org.apache.commons.io.IOUtils;
import java.nio.charset.StandardCharsets;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
def flowFile = session.get()
if (!flowFile) return
//def attributes = flowFile.getAttributes()
flowFile = session.write(flowFile, {inputStream, outputStream ->
  //text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
  try {
     String contentYaml = IOUtils.toString(inputStream, 
String.valueOf(StandardCharsets.UTF_8))
     ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
     Object obj = yamlReader.readValue(contentYaml, Object.class);
     ObjectMapper jsonWriter = new ObjectMapper();
     String 
contentJson=jsonWriter.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
     outputStream.write(contentJson.getBytes(StandardCharsets.UTF_8));
  } catch(JsonProcessingException e){
    flowFile = session.putAttribute(flowFile, 'yaml-error', "yaml format error")
    session.transfer(flowFile, REL_FAILURE)
    return;
  }catch (Exception e){
    flowFile = session.putAttribute(flowFile, 'yaml-error', "convert error")
    session.transfer(flowFile, REL_FAILURE)
    return;
  } 
} as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)
{code}
 

> Add YAML Record Reader
> ----------------------
>
>                 Key: NIFI-11197
>                 URL: https://issues.apache.org/jira/browse/NIFI-11197
>             Project: Apache NiFi
>          Issue Type: New Feature
>          Components: Extensions
>    Affects Versions: 1.20.0
>            Reporter: macdoor615
>            Assignee: Daniel Stieglitz
>            Priority: Major
>             Fix For: 2.0.0-M1, 1.24.0
>
>          Time Spent: 4h 40m
>  Remaining Estimate: 0h
>
> The yaml format is basically equivalent to json. When used as a configuration 
> file, it is much more convenient than json. It can have comments and the file 
> is shorter.
> More and more systems adopt yaml format. Now we developed a conversion tool 
> from yaml to json with the ExecuteGroovyScript processor.
> It is recommended to add a processor that can convert between yaml and json



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to