Itian
Itian

Reputation: 123

How to delete a complete entry from json array at run-time

I want to delete a JSON-array record at run-time by providing the values to remove.

I have tried the following code but it is adding a / on each values;

Original Json File:
{"Products":[{"p10":"SamsungS5"},{"i6":"Iphone6"}]}

After performing the delete operation, the output becomes:
{"Products":[["{\"p10\":\"SamsungS5\"}","{\"i6\":\"Iphone6\"}"]]}

The code for the remove method is:

public static void removeSearchedClass(String value ) throws IOException, ParseException, InvocationTargetException {


        Object obj = new JSONParser().parse(new FileReader(FILE_NAME));
        JSONObject jo = (JSONObject) obj; 


        ArrayList<String> list = new ArrayList<String>();     
        JSONArray solutions = (JSONArray) jo.get("Products");
        int len = solutions.size();
        if (solutions != null) { 
           for (int i=0;i<len;i++){ 
            list.add(solutions.get(i).toString());
           } 
        }
                list.remove(value.trim());

        solutions.clear();
        solutions.add(list);

        jo.put("Products", solutions);

        FileWriter file = new FileWriter(FILE_NAME, false);
        file.append(jo.toString());
        file.flush();
        file.close(); 
         }

When I enter "Iphone6", I need the complete entry for Iphone6, which is {"i6":"Iphone6"}, to be deleted.

Upvotes: 0

Views: 432

Answers (1)

user1803551
user1803551

Reputation: 13427

Using the JSON-P (JSR-374), which is part of the JavaEE (now EE4J) specs, you can you the following:

String jsonString = "{\"Products\":[{\"p10\":\"SamsungS5\"},{\"i6\":\"Iphone6\"}]}";
String removeValue = "\"Iphone6\"";

JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
JsonObject jsonObj = jsonReader.readObject();

JsonPatchBuilder builder = Json.createPatchBuilder();

JsonArray jsonArray = jsonObj.getJsonArray("Products"); // [{"p10":"SamsungS5"},{"i6":"Iphone6"}]
for (int i = 0; i < jsonArray.size(); i++) {
    JsonObject entry = jsonArray.get(i).asJsonObject();
    for (JsonValue value : entry.values()) {
        if (value.toString().equals(removeValue)) {
            builder.remove("/Products/" + i);
        }
    }
}

JsonPatch patch = builder.build();
JsonObject newObj = patch.apply(jsonObj);
System.out.println(newObj); // {"Products":[{"p10":"SamsungS5"}]}

The "Products" array contains JSON entries (a key-value pair, like {"i6":"Iphone6"}). You then iterate over the values of each entry (though there is only one: "Iphone6"). If it matches the one you want to remove, you modify the patch to remove the index of that entry. Then apply the patch on the JSON object.

The dependencies are the specification, like JavaEE:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>8.0</version>
    <scope>provided</scope>
</dependency>

and an implementation of JSON-P, like Glassfish:

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.1.4</version>
</dependency>

Upvotes: 3

Related Questions