How do we parse two flowfiles that are incoming at the same time?
We are currently trying to merge two Json files - one is nested and one is flat:
"ampdata": [
{
"nr": "222",
"code": "XXXXXXXx",
"Anr": "AVAILABLE",
"ability": [ "" ],
"type": "wheeled",
"conns": [
{
"nr": "104",
"status": "",
"version": "3",
"format": "skirt",
"amp": "32",
"vol": "400",
"vpower": 22
}
]
}
[ {
"nr" : 1234556,
"Anr" : "ABC",
"Title" : null,
"Comp" : null,
"Name" : "NAME",
"CompanyName" : "XYZ"
}]
My current Approach is:
var flowFile = session.get();
if (flowFile != null) {
var StreamCallback = Java.type("org.apache.nifi.processor.io.StreamCallback")
var IOUtils = Java.type("org.apache.commons.io.IOUtils")
var StandardCharsets = Java.type("java.nio.charset.StandardCharsets")
flowFile = session.write(flowFile,
new StreamCallback(function(inputStream, outputStream) {
var text = IOUtils.buffer(inputStream)
var obj = JSON.parse(text)
var neu = [];
var neuesObjekt = {};
for (var i = 0; i < obj.ampdata.length; i++) {
var entry = obj.ampdata[i];
if(obj.ampdata[i].nr != obj2.nr) {
obj2.nr = obj.ampdate[i].nr
}
}
outputStream.write(JSON.stringify(newObj, null, '\t').getBytes(StandardCharsets.UTF_8))
}))
flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').split('.')[0]+'_translated.json')
session.transfer(flowFile, REL_SUCCESS)
How do we parse two flowfiles that are incoming at the same time? we need to work with both at the same time to compare them at several positions. we are not sureon how we can avoid overwriting the first flowfile.
we had another Approach with using the MergeConent-Processor, but the result was just the concatenation of the both Jsons in a way that was not a valid Json anymore. Anyway I do prefer the Javascript attempt more, I just need your help in figuring out, how to do it in a proper way.