2

I have such json ArrayNode and I need to remove from each element for example field "xxx" using ObjectMapper, ArrayNode, JsonNode or ObjectNode. But without Gson and @JsonIgnore etc.

"arrayNode": [
               {
                 "xxx": {},
                 "yyy": {}
               },
               {
                 "xxx": {},
                 "yyy": {}
               }
             ]

2 Answers 2

1

I am not sure whether this problem has been solved or not. But following code snippet shows how to remove a field whose key is xxx from JSON node. And a JsonNode cannot perform insertion or deletion, so you have to cast it to ObjectNode for further manipulation.

Code snippet

 ObjectMapper mapper = new ObjectMapper();
 JsonNode rootNode = mapper.readTree(jsonStr);
 rootNode.get("arrayNode").forEach(e -> {
     if (e.has("xxx")) {
         ObjectNode objNode = (ObjectNode) e;
         objNode.remove("xxx");
     }
 });

 System.out.println(rootNode.toString());

Console output

{"arrayNode":[{"yyy":{}},{"yyy":{}}]}

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

Comments

0

You can use this maven dependency : http://mvnrepository.com/artifact/org.json/json/20160212

It's very simple to understated and use. ex:

JSONObject obj = "YOUR_JSON_STRING";
JSONArray result = obj.getJSONArray("YOUR_STRING_KEY");

for(JSONObject elem : result){
   String out = elem.getString("xxx");
}

More you can read at : https://developer.android.com/reference/org/json/JSONArray.html Good luck

2 Comments

I suppose that it would be helpful, but I'm not able to use any libs except Jackson
answer doesn't say anything about removal

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.