Updating an existing json data

This post explain how we can update an existing json data stored in a file. For example purpose lets create a json file named jsondata1.txt with the following data

jsondata1.txt


{
    “firstName”:”John”,
    ”lastName”:”McClane”,
    ”age”:”28″,
    ”address”:{
        “street”:”street1″,
        ”city”:”city1″,
        ”state”:”state1″,
        ”country”:”country1″,
        ”postalCode”:”12345″
    },
    "Phones":[{"Mobile":"111-111-1111"},{"Home":"222-222-2222"}]
}

The code is as shown below


package objectmodel;

import java.io.FileReader;
import java.io.FileWriter;
import java.util.Map;

import javax.json.Json;
import javax.json.JsonBuilderFactory;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonReader;
import javax.json.JsonValue;
import javax.json.JsonWriter;

public class Example4 {
    public static void main(String[] args) throws Exception {
        try(FileReader fr = new FileReader("jsondata1.txt");
            FileWriter fw = new FileWriter("jsondata3.txt");
            JsonReader reader = Json.createReader(fr);
            JsonWriter jsonWriter = Json.createWriter(fw);) {
                JsonObject jsonObject = reader.readObject();

                JsonBuilderFactory jsonBuilderFactory = Json.createBuilderFactory(null);
                JsonObjectBuilder jsonObjectBuilder = jsonBuilderFactory.createObjectBuilder();

                for(String key : jsonObject.keySet()) {
                    jsonObjectBuilder.add(key, jsonObject.get(key));
                }
                jsonObjectBuilder.add("sex", "Male");

                jsonObject = jsonObjectBuilder.build();

                jsonWriter.writeObject(jsonObject);
        }
    }
}

The output get stored in another file named jsondata3.txt with contents as shown below


{
    "firstName":"John",
    "lastName":"McClane",
    "age":"28",
    "address":{
        "street":"street1",
        "city":"city1",
        "state":"state1",
        "country":
        "country1",
        "postalCode":"12345"
    },
    "Phones":[{"Mobile":"111-111-1111"},{"Home":"222-222-2222"}],
    "sex":"Male"
}

In the above code I have read the contents of file “jsondata1.txt” and obtained  the jsonObject which represents the contents in the file.

JsonObject jsonObject = reader.readObject();

Created an instance of JsonObjectBuilder. Obtained the contents of jsonObject by iterating and copying the contents one by one in the builder instance.

JsonBuilderFactory jsonBuilderFactory = Json.createBuilderFactory(null);
JsonObjectBuilder jsonObjectBuilder = jsonBuilderFactory.createObjectBuilder();

for(String key : jsonObject.keySet()) {
jsonObjectBuilder.add(key, jsonObject.get(key));
}

At the end I have added another entry in the builder object

jsonObjectBuilder.add(“sex”, “Male”);

Then called build method to generate a new JsonObject.

jsonObject = jsonObjectBuilder.build();

Finally called jsonWriter object to write the updated content in another file named “jsondata4.txt”.

jsonWriter.writeObject(jsonObject);

2 thoughts on “Updating an existing json data

Leave a Reply