user1175857
user1175857

Reputation: 115

Deleting parent JSON using values in Javascript

Need some JSON help. I'm working with JSON similar to this:

{
    GreatGrandfather: {
        GrandFather: {
            Father: [
                {
                    Son: [
                        {
                            Name: "Abu Nazir",
                            Detail: {
                                Class: "3rd Grade"
                            }
                        },
                        {
                            Name: "Dexter",
                            Detail: {
                                Class: "6th Grade"
                            }
                        },
                        {
                            Name: "Rick",
                            Detail: {
                                Class: "11th Grade"
                            }
                        }
                    ]
                },
                {
                    Son: [
                         {
                            Name: "Tom Walker",
                            Detail: {
                                Class: "3rd Grade"
                            }
                        },
                        {
                            Name: "Sgt Bastisa",
                            Detail: {
                                Class: "6th Grade"
                            }
                        },
                        {
                            Name: "The Governer",
                            Detail: {
                                Class: "11th Grade"
                            }
                        }
                    ]
                }
            ]
        }
    }
}

My question is how do I delete the Sons that have the Class value of "3rd Grade"? So both Abu Nazir and Tom Walker will be deleted and each father will have only 2 sons?

Also, how would I add Sons to both fathers?

{Name: "Stewie",Detail: {Class:"Kindergarden"}}

and

{Name: "Peter",Detail: {Class:"1st Grade"}}

Thanks!

Upvotes: 0

Views: 315

Answers (2)

jbabey
jbabey

Reputation: 46647

In javascript, you'd use JSON.parse to transform your JSON string into a javascript object, and manipulate that:

var parsed = JSON.parse(JSONString);
var fathers = parsed.GreatGrandfather.GrandFather.Father;

// loop through the father array, removing sons in 3rd grade
for (var i = 0; i < fathers.length; i++) {
    var sons = fathers[i].Son;

    for (var j = 0; j < sons.length; j++) {
        // remove the 3rd graders
        if (sons[j].Detail.Class === '3rd Grade') {
            sons.splice(j, 1);
        }
    }
}

// add some sons
var newSon = {
    Name: "Stewie",
    Detail: {
        Class: "Kindergarden"
    }
};

fathers[0].Son.push(newSon);

Upvotes: 1

Bergi
Bergi

Reputation: 664620

var obj = JSON.parse(jsonstring);
obj.GreatGrandFather.GrandFather.Father.forEach(function(father) {
    var sons = father.Son.filter(function(son) {
        return son.Details.Class != "3rd Grade";
    });
    sons.push({Name: "Stewie",Detail: {Class:"Kindergarden"}});
    sons.push({Name: "Peter",Detail: {Class:1st Grade}});
    father.Son = sons;
});
var newjson = JSON.stringify(obj);

Of course you might do it with loops instead of ES5.1-iterator functions, but I think you get the concept.

Upvotes: 1

Related Questions