-2

I need to replace All in s3data Emp_Id with the response data's values(expected output appended), ignore 0 and 1 in response they are not required. S3data is json data I can't use inbuilt function like Object.fromEntries because they are not working in apache nifi execute script. So I have to do this with foreach or with loop so that it can work in nifi. I tried it but it didn't work.

var response = {
    "status": "success",
    "data": [[123, 0], [124, 0], [446, 0], [617, 1], [620, 0], [470 ,1]]
};

var s3Data = {
    "Emp_Id": "All",
    "Emp_loc": 523,
    "Emp_dept": "Management",
    "Emp_sub_dept": "Finance",
    "Emp_sub_dept2": "Accountant"
};
var result={}
var dataName = s3Data[0].Emp_Id;
   response.data.forEach(function(elem, index) {
      if(dataName==='All'){
        result[0].Emp_Id=elem[0];
      }
      
    });

console.log(result);

EXPECTED OUTPUT:

[
  {
    Emp_Id: 123,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 124,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 446,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 617,
    Emp_loc: 523,
   Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 620,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 470,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  }
]

EDITED: So here is a little update in the question I tried to add more data to s3Data with condition that if there is no All inEmp_Id then it will be pushed to result as it is or if it have All then it will be replaced by api response but not getting expected result,

var response = {
    "status": "success",
    "data": [[123, 0], [124, 0], [446, 0], [617, 1], [620, 0], [470 ,1]]
};

var s3Data = [
{
"Emp_Id": "All", 
"Emp_loc": 523, 
"Emp_dept": "Management",
"Emp_sub_dept": "Finance",
"Emp_sub_dept2": "Accountant"
},
{
"Emp_Id": "1230",
"Emp_loc": 522, 
"Emp_dept": "arts", 
"Emp_sub_dept": "Finance",
"Emp_sub_dept2": "Accountant"}
];

var IDs = response.data;
var result=[];

for(var j=0;j<s3Data.length;j++){
var getEmpId = s3Data[j].Emp_Id;
if(getEmpId==='All'){
for (var i = 0; i < IDs.length; i++) {
  var id = IDs[i][0];
  s3Data.Emp_Id = id;
  result.push(s3Data)
}
}else{
    result.push(s3Data);
}
}


console.log(result); 

Expected result for edited part

[
  {
    Emp_Id: 123,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 124,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 446,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 617,
    Emp_loc: 523,
   Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 620,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 470,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 1230,
    Emp_loc: 522, 
    Emp_dept: 'arts', 
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  }
]
dash
  • 35
  • 5
  • 2
    `s3Data[0]` doesn't make sense because `s3Data` is not an array – Konrad May 30 '23 at 06:58
  • @Konrad Then how can I read this value? It is json data right? – dash May 30 '23 at 06:59
  • You just need to do `s3Data.Emp_Id` to call the object property `Emp_id`. `[number]` is only used to call a specific index of an array – Florent M. May 30 '23 at 07:05
  • `It is json data right?` - wrong. JSON is only ever a string – Jaromanda X May 30 '23 at 07:06
  • @JaromandaX So you mean if 523 would be in "523" then it would be a valid json? – dash May 30 '23 at 07:15
  • @dash no. this is JSON: `'{ "Emp_Id": "All", "Emp_loc": 523, "Emp_dept": "Management", "Emp_sub_dept": "Finance", "Emp_sub_dept2": "Accountant" }';` – mplungjan May 30 '23 at 07:47
  • @dash - no, JSON describes a data interchange format that is purely a string [as described here](https://www.json.org/json-en.html) – Jaromanda X May 30 '23 at 07:49
  • How does the ID match the data? What is the expected output now you have data WITH an ID... – mplungjan May 30 '23 at 11:57
  • @mplungjan Actually I have data in these format and I only need to replace with apiResponse whenever there is All in Emp_Id. I thought it will be easy with if else to pass id thats why didn't included that but now I am stucking because not getting expected result..................Appending expected Result as well – dash May 30 '23 at 12:02
  • @mplungjan expected result added – dash May 30 '23 at 12:06

2 Answers2

0

var response = {
  "status": "success",
  "data": [
    [123, 0],
    [124, 0],
    [446, 0],
    [617, 1],
    [620, 0],
    [470, 1]
  ]
};

var s3data = {
  "Emp_Id": "All",
  "Emp_loc": 523,
  "Emp_dept": "Management",
  "Emp_sub_dept": "Finance",
  "Emp_sub_dept2": "Accountant"
};
var result = []
response.data.forEach(function(elem, index) {
  const obj = Object.assign({
    Emp_Id: elem[0]
  }, s3data)
  result.push(
    obj
  );
})
console.log(result);
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • This is correct but actually I have to use only json data, here result=[] is an array and this is giving error in nifi. We can only work with json objects in nifi ecmascript. I dont have knowledge of javascript much as I dont work in this techstack but its a short term requirement. – dash May 30 '23 at 07:13
  • it is giving error on push if I change [] to {} on result – dash May 30 '23 at 07:14
  • But your expected output is an array. You can't `push` into an object. Each item in an object has to have a key – Konrad May 30 '23 at 07:15
  • can't use Object.assign it is giving error :( , in-built functions are not working in nifi execute script thats why I couldn't use map's Object.fromEntries. .... Any other way to assign Object value without using these inbuilt libraraies or functions? – dash May 30 '23 at 07:24
  • `const obj = { ...s3data, Emp_Id: elem[0] }` – Konrad May 30 '23 at 07:30
  • Can you please have a look on edited question block? I am stucking while trying with more data in s3Data. – dash May 30 '23 at 11:24
0

Legacy JS answer

var response = {
    "status": "success",
    "data": [[123, 0], [124, 0], [446, 0], [617, 1], [620, 0], [470 ,1]]
};
var IDs = response.data;

var s3Data = `[{ "Emp_Id": "All", "Emp_loc": 523, "Emp_dept": "Management", "Emp_sub_dept": "Finance", "Emp_sub_dept2": "Accountant" }, { "Emp_Id": "1230", "Emp_loc": 522, "Emp_dept": "arts", "Emp_sub_dept": "Finance", "Emp_sub_dept2": "Accountant"} ]`; // JSON string

var objects = JSON.parse(s3Data);
var resultArray = [];
var template = "";
for (var i = 0; i < objects.length; i++) {
  var id = objects[i].Emp_Id; console.log("id",id)
  var clone = JSON.parse(JSON.stringify(objects[i]));
  if (id !== "All") {
    resultArray.push(clone); // push a clone
  }  
  else if (!template) template = clone;
}  

// continue with the one without ID

for (var i = 0; i < IDs.length; i++) {
  var id = IDs[i][0];
  var obj = JSON.parse(JSON.stringify(template));  // copy
  if (obj.Emp_Id === "All") obj.Emp_Id = id;
  resultArray.push(obj)
}  

console.log(resultArray); // Object array
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Can you please check my updated question block, tried adding more data to s3data but stucked again – dash May 30 '23 at 10:58
  • See update. I hope that is what you meant – mplungjan May 30 '23 at 12:19
  • No, Emp_debt have arts in all of the output, it should be what it was originally in input only Emp_Id will be change, You are misunderstanding it Actually there will be data in hundreds not only these two so I can't do it like this I have to – dash May 30 '23 at 12:23
  • @dash Oops, I forgot to test the ID again second time and to use the templage – mplungjan May 30 '23 at 12:29