0

I have json that looks like this

const myJson = [
  {

  "title":"1. Lockdown Population by Loan (186006)",
  children:[
     {
        title:"1.1 Previous (191225)",
        "firstLevelSubNodes:[
              { 
                title:"1.1.1 Roll forward (178260)" ,
                "firstLevelSubNodes":[{ 
                  title:"1.1.1.1 Roll forward (178260)"
                  }]
              }
        ],
        "secondLevelSubNodes":[
             title:"1.2.1 Bounce forward (178260)" ,
                "firstLevelSubNodes":[{ 
                  title:"1.2.1.1 Dash forward (178260)",
                    "firstLevelSubNodes":[{ 
                     title:"1.2.1.1.1 Skip forward (178260)",
                  }]
                  }]
        ],
        "thirdLevelSubNodes":[
        ],
        "fourthLevelSubNodes":[

        ]
     }         
  ],
  }
]

Everywhere you see the word xxxLevelSubNodes it needs to say the word "children" like below.

 const myJson = [
     {

  "title":"1. Lockdown Population by Loan (186006)",
  children:[
     {
        title:"1.1 Previous (191225)",
        "children:[
              { 
                title:"1.1.1 Roll forward (178260)" ,
                "children":[{ 
                  title:"1.1.1.1 Roll forward (178260)"
                  }]
              }
        ],
        "children":[
             title:"1.2.1 Bounce forward (178260)" ,
                "children":[{ 
                  title:"1.2.1.1 Dash forward (178260)",
                    "children":[{ 
                     title:"1.2.1.1.1 Skip forward (178260)",
                  }]
                  }]
        ],
        "children":[
        ],
        "children":[
        ]
     }         
  ],
  }
]

I have tried to loop through but it renames the word with children however it overwrites the last key. Also it doesn't loop through the nested portion of the JSON. I need it to remain in the exact same structure as above

const res = { firstLevelSubNodes: 'children', secondLevelSubNodes: 'children',

  thirdLevelSubNodes: 'children', fourthLevelSubNodes: 'children' }; 

 const object = JSON.parse(myJson);
    for (const k in res) {

      const newValue = res[k];

      object[newValue] = object[k];

      object[newValue].name = newValue;

      delete object[k];

    }
5
  • 5
    JSON cannot have two properties with same key Commented Feb 21, 2018 at 18:20
  • 2
    Maybe you can create one key children list and add all the xxxLevelSubNodes to it Commented Feb 21, 2018 at 18:21
  • I agree with @user7 but if you really wanted to, I would use JSON.stringify() and then use some regex with string.replace() You would get the result you're describing. Commented Feb 21, 2018 at 18:23
  • Do you want to merge the children arrays for everything that matches? Commented Feb 21, 2018 at 18:24
  • No merging. This represents a tree view in our app. The custom angular directive we use finds the word "children" and identifies it automatically as a child node Commented Feb 21, 2018 at 18:37

1 Answer 1

3

Like the people who commented said, what you are asking for is not possible just because of the way JSON works.

you need to rethink what you are trying to do... "This represents a tree view in our app. The custom angular directive we use finds the word "children" and identifies it automatically as a child node "

By that logic, there should be only 1 children property at each level that means that your json should look like:

let myJson = [
    {

        "title":"1. Lockdown Population by Loan (186006)",
        children:[
            {
                title:"1.1 Previous (191225)",
                "children":[
                   {
                        title:"1.1.1 Roll forward (178260)" ,
                        "children":[{
                            title:"1.1.1.1 Roll forward (178260)"
                        }]
                    },
                    {
                        title: "1.2.1 Bounce forward (178260)",
                        "children": [{
                            title: "1.2.1.1 Dash forward (178260)",
                            "children": [{
                                title: "1.2.1.1.1 Skip forward (178260)",
                            }]
                        }]
                    },
                ],
            }
        ],
    }
];

If this is structure is true, what you really want to do is merge all the properties with a key that contains you format into the same array.

Of course you would want to do this recursively.

So its something like

function refactorJson(array) {
    for (var i =0; i< array.length; i++) {
        var obj = array[i];
        var newObj = {};
        newObj.title = obj.title;
        newObj.children = [];

        for (var property in obj) {
        console.log(property)
            if (property.contains('LevelSubNodes') || property === 'children') {
                var subObj = obj[property];
                var child = refactorJson(subObj);
                if (child) {
                    newObj.children.push(child);
                }
            }

        }
    }
    return newObj;
}





var json =  [{
    "title": "1. Lockdown Population by Loan (186006)",
    "children": [{
        "title": "1.1 Previous (191225)",
        "firstLevelSubNodes": [{
            "title": "1.1.1 Roll forward (178260)",
            "firstLevelSubNodes": [{
                "title": "1.1.1.1 Roll forward (178260)"
            }]
        }],
        "secondLevelSubNodes": [{
            "title": "1.2.1 Bounce forward (178260)",
            "firstLevelSubNodes": [{
                "title": "1.2.1.1 Dash forward (178260)",
                "firstLevelSubNodes": [{
                    "title": "1.2.1.1.1 Skip forward (178260)"
                }]
            }]
        }],
        "thirdLevelSubNodes": [],
        "fourthLevelSubNodes": [

        ]
    }]
}];

var newJson = formatJson(json)
Sign up to request clarification or add additional context in comments.

4 Comments

btw, your json had some other validity issues with it, so i added a test one that works
Also I noticed the return newObj variable exists outside of the loop it was created in
your code works but it is putting an extra [] bracket right after the children node. Do you know why it's doing that? The JSON isn't formatted correctly. It looks like "children": [ [ { } ] ] when it should be "children": [ { } ]
im not sure to what you are refereing to {"title":"1. Lockdown Population by Loan (186006)","children":[{"title":"1.1 Previous (191225)","children":[{"title":"1.1.1 Roll forward (178260)","children":[{"title":"1.1.1.1 Roll forward (178260)","children":[]}]},{"title":"1.2.1 Bounce forward (178260)","children":[{"title":"1.2.1.1 Dash forward (178260)","children":[{"title":"1.2.1.1.1 Skip forward (178260)","children":[]}]}]}]}]} this is what im getting, and the children is an array of objects not arrays as you are describing... can you give me an example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.