How to remove the duplicate info from JSON objects?

sri83
Tera Guru

HI Team,

Please help me on the below requirement.

var object1 =[{

"taskid":"INC5678",

"tasAcco":"Apple","

taskReg":"EMEA"},

"taskid":"INC1234",

"tasAcco":"Apple","

taskReg":"India"},

{"taskid":"Inc4321",

"tasAcco":"Nokia","

taskReg":"USA"

}]

 

var object2 =

[{

"taskid":"INC1234",

"tasAcco":"Apple","

taskReg":"India"},

{"taskid":"Inc4321",

"tasAcco":"Nokia","

taskReg":"USA"

}]

 

In the above 2 JSON data we need ignore the common records from Object1  as the combination of (taskid+tasAcco+taskReg) Remining with one records need to pick and create a incident with the details.

 

Please help me how to active it.

 

1 ACCEPTED SOLUTION

Rahul RJ
Giga Sage
Giga Sage

@sri83  You try the below code which will return a unique value

var object1=[    {
      "taskid": "INC5678",
      "tasAcco": "Apple",
      "  taskReg": "EMEA"
    },  
     { "taskid": "INC1234",
    "tasAcco": "Apple",
    "  taskReg": "India"  },  
    {
    "taskid": "Inc4321",
    "tasAcco": "Nokia",
    "  taskReg": "USA"
  }  ];
  var object2=[  {
    "taskid": "INC1234",
    "tasAcco": "Apple",
    "  taskReg": "India"
  }, 
   {    "taskid": "Inc4321",
    "tasAcco": "Nokia",
    "  taskReg": "USA"  } 
 ];

var arr = [];//to store final result
var flag = 0;
object1.forEach(function(object1){
    object2.forEach(function(object2){
        if(object2.taskid == object1.taskid){
            flag = 1;
        }
    });
    if(flag != 1){
        arr.push(object1);
    }
    flag = 0;
});
gs.info(JSON.stringify(arr));

 

Please mark the answer correct/helpful based on Impact.

Regards,

RJ

View solution in original post

4 REPLIES 4

Ratnakar7
Mega Sage
Mega Sage

Hi @sri83 ,

Please use below script to get unique array:

var duplicateArr = [];
var finalArray = [];
object1.forEach(function (item) {
  var combinedStr = item.taskid + ',' + item.tasAcco+','+item.taskReg;
  if (duplicateArr.indexOf(combinedStr) < 0){
    duplicateArr.push(combinedStr);
    finalArray.push(item);
  }
});
gs.log(JSON.stringify(finalArray));

 

Thanks,

Ratnakar

Hi Ratnakr,

 

Thanks for the quick response.

I want to compare both JSON objects and filter the final data in Object3 to store the Data. so that i will use and create the tickets by using the info.

 

I am Not aware of it how remove the data and get the final data.Please help me on this

 

Rahul RJ
Giga Sage
Giga Sage

@sri83  You try the below code which will return a unique value

var object1=[    {
      "taskid": "INC5678",
      "tasAcco": "Apple",
      "  taskReg": "EMEA"
    },  
     { "taskid": "INC1234",
    "tasAcco": "Apple",
    "  taskReg": "India"  },  
    {
    "taskid": "Inc4321",
    "tasAcco": "Nokia",
    "  taskReg": "USA"
  }  ];
  var object2=[  {
    "taskid": "INC1234",
    "tasAcco": "Apple",
    "  taskReg": "India"
  }, 
   {    "taskid": "Inc4321",
    "tasAcco": "Nokia",
    "  taskReg": "USA"  } 
 ];

var arr = [];//to store final result
var flag = 0;
object1.forEach(function(object1){
    object2.forEach(function(object2){
        if(object2.taskid == object1.taskid){
            flag = 1;
        }
    });
    if(flag != 1){
        arr.push(object1);
    }
    flag = 0;
});
gs.info(JSON.stringify(arr));

 

Please mark the answer correct/helpful based on Impact.

Regards,

RJ

Hi Rahul,

Thanks for the quick reply.

I want to validate 3 parameter at a time as taskId+Account+region if the anyone of the value is different  then i need to pick the record and create a task for this account as well.

 

For ex one task is related with above combination but region USA now another record is having the same data but Region is India then we will pick the record and create a task. 

Please help me on this