I have a JSON treeData that I need to convert into a different format so that I can use a visualization library in JavaScript.
treeData={
"leftNode": {
"leftNode": {
"leftNode": {
"leftNode": {
"leftNode": "fence_brand_name = 'reebok'",
"rightNode": "fence_brand_id = 123",
"centerOperator": "OR"
},
"rightNode": "fence_category_name = 'shoes'",
"centerOperator": "AND"
},
"rightNode": "latitude > 19.1140997",
"centerOperator": "AND"
},
"rightNode": "latitude = 72.89498",
"centerOperator": "AND"
},
"rightNode": "radius = 5000",
"centerOperator": "AND"
}
The depth of this tree can vary, and I need to convert it into the following format:
newTreeData=[{
leftNode=[{
leftNode=[{
leftNode="left"
rightNode="right",
centerOperator="op"
}],
rightNode="right",
centerOperator="op"
}],
rightNode="right",
centerOperator="op"
}]
I have attempted to convert it using the code below, but I haven't had any success yet.
var addNodes=function(data){
if(isObj(data.leftNode)){
var right=data.rightNode;
var center=data.centerOperator;
newTreeData.push(rightNode:right,newCenterOperator:center,leftNode:[]);
addNodes(data.leftNode);
}else{
newTreeData.push(rightNode:data.rightNode,centerOperator:data.centerOperator,leftNode:[]);
}
}
var function isObj(val) {
if (val === null) { return false;}
return ( (typeof val === 'function') || (typeof val === 'object') );
}