Click here to Skip to main content
15,569,696 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
{
"k1" : "v1",
"k2" : ["v2", "v3", "", {"k21": "v21", "k22": ""}],
"k3" : {"k4" : "v4", "k5" : ["v5", "v6", null], "k6": {"k7" : "v7", "k8" : ""}},
"k9" : [],
"k10" : null,
"k11" : [23, true, false, "hello"]
}


What I have tried:

Java
public void filterJson(JsonObject jsonObject){
        Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
        Iterator<Map.Entry<String,JsonElement>>iterator=entrySet.iterator();
        while(iterator.hasNext()){
            Map.Entry<String, JsonElement> entry = iterator.next();
            if (entry.getValue().isJsonArray()){
                JsonArray jsonArray = entry.getValue().getAsJsonArray();
                for(JsonElement ele : jsonArray){
                    if(ele.isJsonObject()){
                        filterJson(ele.getAsJsonObject());
                    }
                }
            }
            if(entry.getValue().isJsonObject()){
                filterJson(entry.getValue().getAsJsonObject());
            }
            if (!entry.getValue().isJsonObject() && !entry.getValue().isJsonArray()){
                if(entry.getValue().isJsonNull() || entry.getValue().equals("null")
                        || entry.getValue().getAsString().trim().equals("")){
                    
                    iterator.remove();
                }
            }
        }
    }
Posted
Updated 19-Mar-22 1:33am
v2
Comments
Richard Deeming 15-Mar-22 4:30am    
You've forgotten to ask a question.

You need to explain precisely what you are trying to do, what you have tried, and where you are stuck.
Maciej Los 19-Mar-22 6:34am    
Well, a question is in the title :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900