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.
accountsis an object itself so yourjsonObjshould point to that object. Besides that please consider adding more information to your question since we can't know whatjsonObjactually is (actual type, which library, what does it point to etc.) The more effort you put into the question the more people will be willing to help. - Thomas