I have the JSON below:
{"response":{"result":{"Leads":{"row":[{"LEADID":"849730000000063017","SMOWNERID":"849730000000061001"},{"LEADID":"849730000000063015","SMOWNERID":"849730000000061001","HIII":"hello"},{"LEADID":"849730000000063007","SMOWNERID":"849730000000061001","BYEE":"tata"},{"LEADID":"849730000000063005","SMOWNERID":"849730000000061001"},{"LEADID":"849730000000063003","SMOWNERID":"849730000000061001"},{"LEADID":"849730000000063001","SMOWNERID":"849730000000061001"}]}},"uri":"/crm/private/json/Leads/getMyRecords"}}
I have the requirement to get the JSON path, for which I used the code below:
var str={"response":{"result":{"Leads":{"row":[{"LEADID":"849730000000063017","SMOWNERID":"849730000000061001",},
{"LEADID":"849730000000063015","SMOWNERID":"849730000000061001"},
{"LEADID":"849730000000063007","SMOWNERID":"849730000000061001","HIII":"hello"},
{"LEADID":"849730000000063005","SMOWNERID":"849730000000061001","BYEE":"tata"},
{"LEADID":"849730000000063003","SMOWNERID":"849730000000061001"},
{"LEADID":"849730000000063001","SMOWNERID":"849730000000061001"}]}},
"uri":"/crm/private/json/Leads/getMyRecords"}}
var keys = [];
getKeys(keys,str, '');
for(var i=0; i<keys.length; i++) {
var d=new Array();
d=keys[i][1].replace(/^\.|\.$/g, '')
console.log(keys[i][0] + '=' +d)
}
function getKeys(keys, obj, path) {
for(key in obj) {
var currpath = path+'.'+key;
keys.push([key, currpath]);
if(typeof(obj[key]) === 'object') {
getKeys(keys, obj[key], currpath);
}
}
}
Below is the output:
response=response
result=response.result
Leads=response.result.Leads
row=response.result.Leads.row
0=response.result.Leads.row.0
LEADID=response.result.Leads.row.0.LEADID
SMOWNERID=response.result.Leads.row.0.SMOWNERID
1=response.result.Leads.row.1
LEADID=response.result.Leads.row.1.LEADID
SMOWNERID=response.result.Leads.row.1.SMOWNERID
HIII=response.result.Leads.row.1.HIII
2=response.result.Leads.row.2
LEADID=response.result.Leads.row.2.LEADID
SMOWNERID=response.result.Leads.row.2.SMOWNERID
BYEE=response.result.Leads.row.2.BYEE
3=response.result.Leads.row.3
LEADID=response.result.Leads.row.3.LEADID
SMOWNERID=response.result.Leads.row.3.SMOWNERID
4=response.result.Leads.row.4
LEADID=response.result.Leads.row.4.LEADID
SMOWNERID=response.result.Leads.row.4.SMOWNERID
5=response.result.Leads.row.5
LEADID=response.result.Leads.row.5.LEADID
SMOWNERID=response.result.Leads.row.5.SMOWNERID
uri=response.uri
The array elements keys are repetitive (ie) LEADID and SMOWNERID are repetitive in the array. I want to remove the duplicates of the entire array and display the output like this:
response=response
result=response.result
Leads=response.result.Leads
row=response.result.Leads.row
0=response.result.Leads.row.0
LEADID=response.result.Leads.row.0.LEADID
SMOWNERID=response.result.Leads.row.0.SMOWNERID
HIII=response.result.Leads.row.0.HIII
BYEE=response.result.Leads.row.0.BYEE
uri=response.uri
I am stuck here, any help regarding this will be very helpful.
Modify the function getKeys to this :
function getKeys(keys, obj, path) {
for(key in obj) {
var currpath = path+'.'+key;
keys.push([key, currpath]);
if(typeof(obj[key]) === 'object') {
if(obj[key] instanceof Array){ //added check for array
keys.push(['0', currpath+'.0']);
getKeys(keys, obj[key][0], currpath+'.0');
}
else
getKeys(keys, obj[key], currpath);
}
}
}
function getKeys(keys, obj, path) {
for(key in obj) {
var currpath = path+'.'+key;
addKeys(key, currpath);
//keys.push([key, currpath]);
if(typeof(obj[key]) === 'object') {
getKeys(keys, obj[key], currpath);
}
}
}
function addKeys(key, currpath) { //check before adding
present = keys.map(function(x){return(x[1].split('.').slice(-1)[0])});
if (present.indexOf(key)==-1){
keys.push([key, currpath]);
}
}
The answer is not exactly like you want but is the closest I could get. Also the values you have given for HIII and BYEE are wrong. Lastly it is possible to have same keys at different levels, but I am only matching the name of key. It may not work if same keyname is used at different levels.
Another approach is to change the keys to object from array - change the var keys = [] to var keys = {} and then change the getKeys method as follow:
function getKeys(keys, obj, path) {
for(key in obj) {
var currpath = path+'.'+key;
if(keys[key])
continue;
keys[key] = currpath;
if(typeof(obj[key]) === 'object') {
getKeys(keys, obj[key], currpath);
}
}
}
Now you will also need to change the way you print the values after the return from getKeys since now keys is not an array rather an object.