Vimlan G
Vimlan G

Reputation: 21

Change the structure of nested JSON

I m trying to change the structure of the json file. Below is the function that is being used for the current structure. I m trying to change the current function so the right and left key of the json will be merged as child. However, I m facing difficulty with it. Can you guys help me to modify the code or suggest an efficient way to perform the function?

var buildTree = function(jsonObj){

      if(!jsonObj)
          return;
      for(var n in jsonObj){
          that.topicList.push(n);
          return{
                key : n,
                right : buildTree(jsonObj[n][0]),
                left : buildTree(jsonObj[n][1])
          }
      }
  }

The input for this code:

{
"math": [{
    "Math": []
}, {
    "A Greek–English Lexicon": [{
        "A-list": []
    }, {
        "ASCII": []
    }]
}]
}

Current output:

{
"key": "math",
"right": {
    "key": "Math"
},
"left": {
    "key": "A Greek–English Lexicon",
    "right": {
        "key": "A-list"
    },
    "left": {
        "key": "ASCII"
    }
}
}

I want to change the above output into the one like below:

{
"name": "math",
"child": [
  {
    "name": "Math",
    "children" :[]
},
{
    "name": "A Greek–English Lexicon",
    "child": [
      {
        "name": "A-list",
        "child" : []
        },
        {
        "name": "ASCII",
        "child" : []
        }
    ]
}
]}

Upvotes: 1

Views: 211

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386520

This is a recursive approach, which returns a new object.

var object = { "math": [{ "Math": [] }, { "A Greek–English Lexicon": [{ "A-list": [] }, { "ASCII": [] }] }] },
    newObject = {};

function restyle(obj) {
    var k = Object.keys(obj)[0];
    return {
        key: k,
        child: obj[k].map(restyle)
    };
};

newObject = restyle(object);
document.write('<pre>' + JSON.stringify(newObject, 0, 4) + '</pre>');

This is a recursive approach, which changes the object in situ.

function restyle(o) {
    Object.keys(o).forEach(function (k) {
        o.key = k;
        o.child = o[k];
        delete o[k];
        o.child.forEach(restyle);
    });
};

var object = { "math": [{ "Math": [] }, { "A Greek–English Lexicon": [{ "A-list": [] }, { "ASCII": [] }] }] };
restyle(object);
document.write('<pre>' + JSON.stringify(object, 0, 4) + '</pre>');

Upvotes: 1

prajnavantha
prajnavantha

Reputation: 1131

I wrote a solution for this. You basically need to do recursive programming. You may need to do some basic changes if there are errors but essentially i've written the logic and the code. It will recursively parse through the child elements until it finds an empty array, i.e the leaf node. Im assuming there will always be only two child since it looked like a simple tree.

            /*Initially the object is pased here*/
            function parse(obj){
                /*Im assuming that the object has a key and value you need to add other failure checks here*/
                var keys =Object.keys(obj)
                return {
                    "name": keys[0]
                    "child" getChilds(obj[keys[0]])
                }
            }

            /*This is a recursive function which will grab left and right child and finally return the output.*/
            function getChilds(arr){


                    if(arr.length === 0){
                        return []
                    }
                    var obj = arr[0]
                    var keys =Object.keys(obj)


                    var newObj = {}
                    /*left child*/
                    var left = {
                        "name":keys[0],
                        "child":getChilds( obj[keys[0]] )
                    }

                    var obj = arr[1]
                    var keys =Object.keys(obj)
                    /*right child*/
                    var right = {
                        "name":keys[0],
                        "child":getChilds( obj[keys[0]] )
                    }

                    return [left,right]


            }

Upvotes: 0

Related Questions