I haven't tested this, but I think something like this would do what you want:
import org.codehaus.jackson.node.ObjectNode;
// ...
for (JsonNode personNode : rootNode) {
if (personNode instanceof ObjectNode) {
ObjectNode object = (ObjectNode) personNode;
object.remove("familyName");
object.remove("middleName");
}
}
You could also do this more efficiently using Jackon's raw parsing API, but the code would be a lot messier.
Answer from gsteff on Stack OverflowI haven't tested this, but I think something like this would do what you want:
import org.codehaus.jackson.node.ObjectNode;
// ...
for (JsonNode personNode : rootNode) {
if (personNode instanceof ObjectNode) {
ObjectNode object = (ObjectNode) personNode;
object.remove("familyName");
object.remove("middleName");
}
}
You could also do this more efficiently using Jackon's raw parsing API, but the code would be a lot messier.
ObjectMapper of Jackson gives solution with only few steps.
Save the json data in a file say 'data.json'. Copy following the code into a function without import statements and invoke the function. The resulting JSON will be written into a new file 'data1.json'.
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(new File("data.json"));
for (JsonNode node : jsonNode) {
((ObjectNode)node).remove("familyName");
((ObjectNode)node).remove("middleName");
}
objectMapper.writeValue(new File("data1.json"), jsonNode);
If jsonNode is an Array of Objects then jsonNode.elements() returns Iterator<JsonNode>, by using if condition check the node with name equals 1 then delete the entire node
JsonNode jsonNode = objectMapper.readTree(new File("savefiles.json"));
Iterator<JsonNode> nodes = jsonNode.elements()
while(nodes.hasNext()) {
if(nodes.next().get("name").textValue().equals("1")){
nodes.remove();
}
}
If you want to remove an element from the ArrayNode, just
final JsonNode json = objectMapper.readTree(new File("savefiles.json"));
if (json.isArray()) {
for (final Iterator<JsonNode> i = json.elements(); i.hasNext(); ) {
final JsonNode jsonNode = i.next();
if ("1".equals(jsonNode.get("name").asText())) {
i.remove();
}
}
}
This will not create a new instance. The original TreeNode (which is an ArrayNode) is maintained.
Note also the .asText() while comparing.
A Stream version is
final ArrayNode filtered =
StreamSupport.stream(json.spliterator(), false)
.filter(e -> !"1".equals(e.get("name").asText()))
.collect(Collector.of(
objectMapper::createArrayNode,
(array, element) -> array.add(element),
(result, toMerge) -> result.addAll(toMerge)
));
you are removing the key-value pair from JsonObject and not the enclosing JsonArray. And a JsonObject can have several key-value pair in itself. So deleting one doesn't delete the whole object.
jsonObj={"a":"cde","b":"fgh"}
this is a valid jsonObject. when I delete "b" from it like jsonObj.remove("b"), only "a" remains {"a":"cde"} and deleting "a" returns {}
Hence, if you want to remove the whole jsonObject from jsonArray, remove it directly from jsonArray.
And in JsonArray, you can't select a jsonObject with key and rather with its index. Hence, even when it has two JsonObjects with same keys, you have to select one by their unique index.
Try to handle base case for object. It's below code might be help:
public static Map<String, Object> toMap(JSONObject object) throws
JSONException {
if(object==null)return null;
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> keysItr = object.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Object value = object.get(key);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
if(null != value)
map.put(key, value);
if(null==value || value.toString().equalsIgnoreCase("NULL")) {
object.remove(key);
}
}
return map;
}
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":{}}]}
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