UltraSonja
UltraSonja

Reputation: 891

Java: Gson change property name without serialization

Is it possible to change the name of a Json property without serialization with Gson? For example, given this Json

{
   "1": {
      ...
   },
   "2": {
      ...
   }
}

could I change the "1" to a "3" without removing its contents. I know that the addProperty method adds a new property, or overwrites an existing property with a new value, but I want to change the name of a property without affecting its value. Also, pasting the existing value as the second argument of addProperty will not suffice.

EDIT: To add more context, I will explain the bigger picture. I have a JSON string that is a couple thousand lines long. I'm writing a program leveraging Gson in order to change the values in that JSON string. I am at a point where I not only want to change the values of properties, but the names of the properties themselves. I have done everything so far without serialization.

Here is a snippet of the Java I wrote:

String file = "\\temp.json";

FileReader reader = new FileReader(file);

JsonStreamParser parser = new JsonStreamParser(reader);
// Parse entire JSON
JsonElement element = parser.next();
// Get root element
JsonObject sites = element.getAsJsonObject();
// Get first child element
JsonObject site1 = sites.getAsJsonObject("1");

JsonObject clust1 = site1.getAsJsonObject("CLUST");

for(int i = 1; i < 12; i++) {
    // "Dynamic" variable        
    String num = Integer.toString(i);
    // Get property whose name is a number, has siblings
    JsonObject one = custCluster1.getAsJsonObject(num);

    one.getAsJsonObject().addProperty("name", "cluster" + i);

    JsonObject subOne = one.getAsJsonObject("SUB");

    subOne.getAsJsonObject().addProperty("name", "aName" + i);

    for(int n = 1; n < 1002; n++) {
        // "Dynamic" variable
        String inst = Integer.toString(n);
        // Get property whose name is a number, has siblings
        JsonObject subSub = subOne.getAsJsonObject(inst);
        // If the property doesn't exist, then don't execute
        if(subSub != null) {

            JsonArray subSubArray = subSub.getAsJsonArray("SUBSUB");

            subSub.getAsJsonObject().remove("name");

            int m = 0;

            while(m < subSubArray.size()) {

                subSubArray.get(m).getAsJsonObject().remove("SR");

                subSubArray.get(m).getAsJsonObject().remove("FI");

                subSubArray.get(m).getAsJsonObject().remove("IND");

                subSubArray.get(m).getAsJsonObject().addProperty("ST", "1");

                subSubArray.get(m).getAsJsonObject().addProperty("ID", "2");

                subSubArray.get(m).getAsJsonObject().addProperty("DESCR", "hi");

                m++;
            }
            m = 0;
        }
    }
}

Upvotes: 1

Views: 3273

Answers (1)

UltraSonja
UltraSonja

Reputation: 891

Thanks to @mmcrae for helping and suggesting this method.

Since I'm already saving the (key, value) pairs in variables, you can remove the property whose name you want to change from the parent, and then add it back with a new name and the content that was already saved.

Like this:

JsonObject sites = element.getAsJsonObject();

JsonObject site1 = sites.getAsJsonObject("1");

JsonObject clust1 = site1.getAsJsonObject("CLUST");

site1.remove("CLUST");

site1.add("NEWCLUST", clust1);

Upvotes: 2

Related Questions