lundi 28 mars 2016

Filter through nested JSON object and obtain JSON with specific keys, using Ruby

I currently have a nested JSON object which resembles

{
"People": [
    {
        "Name": "James",
        "Age": "18",
        "Gender": "Male",
        "Sports": []
    },
    {
        "Name": "Sarah",
        "Age": "19",
        "Gender": "Female",
        "Sports": [
            "Soccer",
            "Basketball",
            "Football"
        ]
    }
] 
}

Being new to Ruby, I aim to filter throught the entire json and return only the json object/objects in which the "Sports" array has content. So in the above scenario I expect to obtain the object below as a final outcome:

    {
        "Name": "Sarah",
        "Age": "19",
        "Gender": "Female",
        "Sports": [
            "Soccer",
            "Basketball",
            "Football"
        ]
    }

Will I have to initiate a new method to perform such an act? Or would using regular ruby calls work in this case?

Aucun commentaire:

Enregistrer un commentaire