Gson – How to update JSON value example shows how to update a value for a JSON key using the Gson library in Java. The example also shows how to update value using JsonObject without converting JSON to POJO.
How to modify JSON value?
Many times we want to update or modify the value for a specific JSON key. The Gson library provides methods for doing exactly that. To update the value of a specific key, we can use the addProperty
method of the JsonObject class.
1 2 |
public void addProperty(java.lang.String property, java.lang.String value) |
The addProperty
method adds the provided key and value to the JsonObject. What the Gson JavaDoc does not say is that it replaces the value if the key already exists! So we can utilize this method to modify the value.
For this example, I am going to use the below given JSON.
1 2 3 4 5 |
{ "name": "Bob", "age": 23, "department":"IT" } |
Let’s update/modify the department of this employee JSON.
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 GsonUpdateValueExample { 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); /* * Since the department key already exists, the * addProperty method will update the value of it. */ jsonObject.addProperty("department", "HR"); System.out.println("Modified JSON: " + jsonObject); } } |
Output
1 2 |
Original JSON: {"name":"Bob","age":23,"department":"IT"} Modified JSON: {"name":"Bob","age":23,"department":"HR"} |
As we can see from the output, the department key is updated with the new value HR.
The addProperty
method is overloaded to accept String, Number, Boolean, and Character data types as the new value.
We can also the modify department value from a string to a JSON array, but in this case, we need to use the add
method instead of the addProperty
method.
1 2 |
public void add(java.lang.String property, JsonElement value) |
The add
method accepts a JsonElement object as a value, so we can put an array or an object using this method.
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 |
package com.javacodeexamples.gsonexamples; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; public class GsonUpdateValueExample { 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); /* * Modifying department value from string * to an array using the add method. */ JsonArray deptArray = new JsonArray(); deptArray.add("Support"); deptArray.add("ERP"); jsonObject.add("department", deptArray); System.out.println("Modified JSON: " + jsonObject); } } |
Output
1 2 |
Original JSON: {"name":"Bob","age":23,"department":"IT"} Modified JSON: {"name":"Bob","age":23,"department":["Support","ERP"]} |
As we can see from the output, the department key now has an array as a value instead of a string.
Please let me know your views in the comments section below.