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);
for (JsonNode personNode : rootNode) {
if (personNode instanceof ObjectNode) {
if (personNode.has("showTimes")) {
ObjectNode object = (ObjectNode) personNode;
object.remove("showTimes");
}
}
}
Something like this should work (I'm not a Jackson user, so YMMV):
((ObjectNode) movieListElement).remove("showTimes");
EDIT:
JsonNode movieListElement = ((ArrayNode) root.path("movieList").get(index);
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
The change node will allow you to remove key value pairs from a the msg object.

From the info sidebar:
Set, change, delete or move properties of a message, flow context or global context.
The node can specify multiple rules that will be applied in turn.
The available operations are:
- Set - set a property. The value can be a variety of different types, or can be taken from an existing message or context property.
- Change - search & replace parts of the property. If regular expressions are enabled, the replace with property can include capture groups, for example $1. Replace will only change the type if there is a complete match.
- Delete - delete a property.
- Move - move or rename a property.
Try running your JSON through this function:
const jsonToTrim = {
"_id": "ProductionData1",
"_rev": "1-b4a160f5e13fab074d2fcc359e820d3f",
"topic": "",
"payload": {
"plant Operating Time": "600",
"plant stop": "0"
}
};
function trimJSON(json, propsToRemove) {
propsToRemove.forEach((propName) => {
delete json[propName];
});
}
// call the function
trimJSON(jsonToTrim, ['_id', '_rev', 'topic']);
//inspect result
console.log(jsonToTrim);
This is the part that actually removes a property from your object:
delete json[propName];
You can read more about delete here.
As far as the json[propName] is concerned it's an alternative way to writing json.propName and it allows us to have props names coming in dynamically (from an array).