0

I need to remove the complete JSON object by its key name, I have random JSON every time, I need to find a particular element and remove it. for example, I have the following JSON:

{
    "abc":{
        "remove_me": "123456"
    }
}

but may next time I may have the following

{
    "abc": {
        "xyz": {
            "test": "test"
        }
    },
     "abc1": {
        "xyz1": {
            "remove_me": "232233",
            "test": "dfefd",

        }
    }
}

every time I have a different JSON format, even in more complex formats. but the key I have to remove the element is remove_me

I am doing it in Java, please help!

4
  • Are you using a JSON-parsing library? Commented Jul 4, 2020 at 1:15
  • Which JSON library do you use? Commented Jul 4, 2020 at 4:03
  • as an option, you could implement logic by yourself: convert json into Map<String, Object>, then traverse map similar to deep first search logic (checking whether map's value is list, map or any other object, and invoking method recursively), and if key during traversing is remove_me, you need to remove it. Commented Jul 4, 2020 at 4:40
  • I am using com.fasterxml.jackson. Commented Jul 4, 2020 at 18:47

2 Answers 2

1

You can do something like this. This will iterate and remove each matched key.

private static void iterateJSONAndRemoveKey(JSONObject jObj) {
    Set<String> keys = jObj.keySet();
    for(String key : keys) {
        if(key.equals("remove_me")) {
            jObj.remove(key);
        }
        else if(jObj.get(key) instanceof JSONObject) {
            JSONObject subObject = (JSONObject) jObj.get(key);
            iterateJSONAndRemoveKey(subObject);
        }
        else if(jObj.get(key) instanceof JSONArray) {
            JSONArray jArray = (JSONArray) jObj.get(key); 
            for(int i = 0; i < jArray.size(); i++) {
                if(jArray.get(i) instanceof JSONObject) {
                iterateJSONAndRemoveKey((JSONObject)jArray.get(i));
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

You might want to check that elements in arrays are objects too. Otherwise if the JSON contains, for example, [1, 2, 3], you'll get an error. You might also want to handle arrays containing other arrays.
how can I use this by using I am using com.fasterxml.jackson library? I apologize, I did not mention the library earlier.
Thank you very much, that worked for me by making two small changes.** 1) add return statement after jObj.remove(key), 2) used length() instead of jArray.size() in for loop. ** Many Thanks! below is the complete code.
1

As I was using com.fasterxml.jackson Library, Below is the solution.

private static void iterateJSONAndRemoveKey(JsonNode rootNode,String toRemove) throws Exception {
        Iterator<String> keysIterator = rootNode.fieldNames();
        while (keysIterator.hasNext()) {
            String key = keysIterator.next();
            if (key.equalsIgnoreCase(toRemove)){
                ((ObjectNode) rootNode).remove(toRemove);
                return;
            }else if (rootNode.isObject()){
                   iterateJSONAndRemoveKey(rootNode.get(key),toRemove);
            }else if (rootNode.isArray()){
                ArrayNode arrayNode = (ArrayNode) rootNode;
                for (JsonNode node : arrayNode) {
                    iterateJSONAndRemoveKey(node,toRemove);
                }

            }
        }
    }

Asked answer helped me to write the logic in Jackson library.

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.