0

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!

3
  • 5
    json is just text. you decode to whatever your host platform/language's native data types is, then do the manipulations there. then re-encode to json. directily manipulating the json string will probably just corrupt the whole thing. Commented Nov 26, 2012 at 16:03
  • do you have something against 3rd graders? Commented Nov 26, 2012 at 16:07
  • How I would do this in javascript/jquery? Commented Nov 26, 2012 at 16:07

2 Answers 2

1
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.

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

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.