Gson – Remove key from JSON example shows how to remove a key from JSON using the Gson library in Java. The example also shows how to remove a nested key from the JSON object.
How to remove a key value from JSON?
We can use the remove
method of the JsonObject class to remove a key-value pair from the JSON.
1 |
public JsonElement remove(java.lang.String property) |
The remove
method removes the specified key from the JsonObject and returns the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.javacodeexamples.gsonexamples; import com.google.gson.Gson; import com.google.gson.JsonObject; public class GsonRemoveKeyExample { public static void main(String[] args) { String jsonData = "{\"name\": \"Bob\",\"age\": 23,\"department\":\"IT\"}"; //Parse JSON from string JsonObject jsonObject = new Gson().fromJson(jsonData, JsonObject.class); System.out.println("Original JSON: " + jsonObject); /* * To remove a key from JsonObject, use the * remove method and specify the key we want * to delete. */ jsonObject.remove("department"); System.out.println("Modified JSON: " + jsonObject); } } |
Output
1 2 |
Original JSON: {"name":"Bob","age":23,"department":"IT"} Modified JSON: {"name":"Bob","age":23} |
As we can see from the output, the department key and its value are removed from the JSON.
The above example removed the key that is located at the root level. How about the nested key? In that case, we first need to traverse to the object level and then use the remove
method to remove the key. Consider the below given JSON.
1 2 3 4 5 6 7 8 |
{ "name": "Bob", "age": 23, "department": { "name": "IT", "position": {"role": "lead", "years": 3} } } |
If we want to remove the “years” property, we first need to traverse to the “position” object and then use the remove
method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.javacodeexamples.gsonexamples; import com.google.gson.Gson; import com.google.gson.JsonObject; public class GsonRemoveKeyExample { public static void main(String[] args) { String jsonData = "{\"name\": \"Bob\",\"age\": 23,\"department\": {\"name\": \"IT\",\"position\": {\"role\": \"lead\", \"years\": 3}}}"; //Parse JSON from string JsonObject jsonObject = new Gson().fromJson(jsonData, JsonObject.class); System.out.println("Original JSON: " + jsonObject); /* * In order to remove the "years" key, we * first need to traverse and get the position * object from the root object. */ JsonObject positionObject = jsonObject .get("department").getAsJsonObject() .get("position").getAsJsonObject(); /* * now we can remove the years key from * the position object */ positionObject.remove("years"); System.out.println("Modified JSON: " + jsonObject); } } |
Output
1 2 |
Original JSON: {"name":"Bob","age":23,"department":{"name":"IT","position":{"role":"lead","years":3}}} Modified JSON: {"name":"Bob","age":23,"department":{"name":"IT","position":{"role":"lead"}}} |
Please let me know your views in the comments section below.