How to remove values from json array?

I have a json file consisting of a single array. the array consists of objects with the keys name, title, pay, age. How can I delete an entire object by the value of the Name key? I am using the “simple json” library, it is quite old, but it suits my purposes. Sorry, there is a lot of code, I don’t know what to show. Thank.

  public class Json {

   static JSONArray jsonArray;
   public static void JsonWriter(String filePath, String fullname, String age, String pay, String post) {
      try {
         JSONObject worker = new JSONObject();
         worker.put("name", fullname);
         worker.put("age", new Integer(age));
         worker.put("post", post);
         worker.put("pay",  new Integer(pay));

         jsonArray.add(worker);

         FileWriter file = new FileWriter(filePath);
         file.write(jsonArray.toString());
         file.flush();
         file.close();

      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   //add object
   public static void AddWorker() throws IOException, ParseException {
      System.out.println("input name");
      String name = handleInput();
      System.out.println("input post");
      String post = handleInput();
      System.out.println("input age");
      String age = handleInput();
      System.out.println("input pay");
      String pay = handleInput();
      System.out.println("succes");
      JsonWriter("res\Worker List.json", name, age, pay, post);
      WorkerList();
   }
   public static void DeleteWorker()
   {
      System.out.println(ansi().fg(RED).a("Input name").reset());
      String name = handleInput();

   }

   //add object
   public static void WorkerList() throws IOException, ParseException {
      Iterator i = jsonArray.iterator();
      while (i.hasNext())
      {
         System.out.println(Menu.Separator);
         JSONObject innearObj = (JSONObject) i.next();
         System.out.println("name: " + innearObj.get("name") + lineSeparator() +
                 "age: " + innearObj.get("age") + lineSeparator() +
                 "post: " + innearObj.get("post") + lineSeparator() +
                 "pay: " + innearObj.get("pay"));
      }
      System.out.println(Menu.Separator);
      System.out.println("Add new object?" + lineSeparator() + "0 - Yes" + lineSeparator() + "1 - No");
      switch(handleInput())
      {
         case ("0"):
            AddWorker();
            break;
      }
   }

   //Parsing json file
   public static void WorkersParser(String filePath)
   {
      try {
         JSONParser jsonParser = new JSONParser();
         Object obj = jsonParser.parse(new FileReader(filePath));
         jsonArray = (JSONArray) obj;
         WorkerList();
      } catch (IOException | ParseException e)
      {
         e.printStackTrace();
      }
   }
}

 public static String handleInput() {
    try {
        String input;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        input = reader.readLine();
        return input;
    }
    catch (Exception ignored) { }
    return "";
}

////

   [
  {
    "post":"post1",
    "name":"name1",
    "pay":1,
    "age":1
  },
  {
    "post":"post2", 
    "name":"name2", //if name = "post2", delete // {
    "pay":2,                                    //    "post":"post2",
    "age":2                                     //    "name":"name2",
  }                                             //    "pay":"2",
]                                               //    "age":2
                                                // }

Answer

You can loop all of them, check for that parameter and remove() by the index:

String jsonString =  "[{"post":"post1","name":"name1","pay":1,"age":1},{"post":"post2","name":"name2","pay":2,"age":2}]";
JSONArray arr = new JSONArray(jsonString);
String wordToDeleteBy = "name1";
System.out.println(arr.length()); //prints 2
for (int i = 0; i<arr.length(); i++) {
    JSONObject obj = arr.getJSONObject(i);
    if (obj.has("name") && obj.get("name").equals(wordToDeleteBy)) {
        arr.remove(i);
    }
}
System.out.println(arr.length()); //prints 1

Leave a Reply

Your email address will not be published. Required fields are marked *