I have JsonObject like below
{"status":"ACTIVE","accounts":{"email":"[email protected]","name":"Test"}}
How can I remove Json key "email" and its value from the JsonObject by using something like jsonObj.remove("email")
in java
JsonObj.removev working for me if I need to remove status key jsonObj.remove("status")
Update
Some more background, This is for mainly testing a rest end point. In my test I created java object matching to payload with Builder pattern and then covert to Json using GsonBuilder like
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
public class JsonConvertor() {
public static JsonObject convertToJsonObject(Object payload) {
GsonBuilder builder = new GsonBuilder();
return (JsonObject) builder.setFieldNamingPolicy(FieldNamingPolicy.Policy).
create().toJsonTree(payload);
}}
If I need to remove a required field I use, JsonObj.remove("key") on Json Object created by above function.
See Question&Answers more detail:os