use of com.github.wnameless.json.flattener.FlattenMode in project nifi by apache.
the class FlattenJson method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
final String mode = context.getProperty(FLATTEN_MODE).getValue();
final FlattenMode flattenMode = getFlattenMode(mode);
String separator = context.getProperty(SEPARATOR).evaluateAttributeExpressions(flowFile).getValue();
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
session.exportTo(flowFile, bos);
bos.close();
String raw = new String(bos.toByteArray());
final String flattened = new JsonFlattener(raw).withFlattenMode(flattenMode).withSeparator(separator.charAt(0)).withStringEscapePolicy(() -> StringEscapeUtils.ESCAPE_JAVA).flatten();
flowFile = session.write(flowFile, os -> os.write(flattened.getBytes()));
session.transfer(flowFile, REL_SUCCESS);
} catch (Exception ex) {
session.transfer(flowFile, REL_FAILURE);
}
}
Aggregations