I need to alter a existing JSON with few new fields. Example existing JSON
{ "a":1, "b":3 , "c":5 }
I want to add new field after "b":3
{ "a":1, "b":3, "s":4, "c":5 }
When I try adding a new field, it gets added over the end of the existing message like below
{ "a":1, "b":3, "c":5, "s":4 }
Please help me with right approach.
Updated code used for adding new node
Object obj = parser.parse(jsonStr);
JsonObject jsonObject = (JsonObject) obj;
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jsonElement = gson.toJsonTree(jsonObject);
jsonElement.getAsJsonObject().addProperty("s", 4);
jsonStr = gson.toJson(jsonElement);