-2

my JSON is :

{
  "errorMessages" : [ {
    "key" : "QUIESCE_FAILURE",
    "code" : "12345",
    "description" : "User already exists aaa",
    "reason" : "Username {user} is already added aaa",
    "resolution" : "Please provide a different username aaa",
    "more_information" : "more info aaa",
    "type" : "error aaa"
  }, {
    "key" : "DUPLICATE_USERS",
    "code" : "3114587",
    "description" : "Failed to quiesce database(s)",
    "reason" : "Database {database} on host {host} is not accessible",
    "resolution" : "Please check that the database is accessible",
    "more_information" : "kkkk",
    "type" : "error"
  } ]
}

i want to delete json with code 3114587 using jackson.

may i get any suggestion?

Thanks in advance.

Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124
  • What have you tried? Post your code. – Alexis Dufrenoy Oct 30 '18 at 09:39
  • 1
    Have a look at:https://stackoverflow.com/questions/15603033/removing-json-elements-with-jackson – Prashant Pimpale Oct 30 '18 at 09:43
  • 1
    de-serialise the data to a list, do an operation on it to remove the desired list item just like in any other list variable, then re-serialise the changed list back to JSON. JSON is a text-based transmission/storage format, you shouldn't attempt to manipulate it directly. – ADyson Oct 30 '18 at 09:52
  • Have a look at:stackoverflow.com/questions/15603033/… – Prashant Pimpale I want to delete one complete JSON element not any particular field in json . after this output should be { "errorMessages" : [ { "key" : "QUIESCE_FAILURE", "code" : "12345", "description" : "User already exists aaa", "reason" : "Username {user} is already added aaa", "resolution" : "Please provide a different username aaa", "more_information" : "more info aaa", "type" : "error aaa" } ] } – yashaswini jayshankar Oct 30 '18 at 10:49
  • @yashaswinijayshankar the principle is still the same. The "element" you refer to is just an object with an array. It's still one particular index / field within that array. You might need a particular different method since it's not a named property, but perhaps you can research what that method is called (sorry I only know JSON and a tiny bit of Java, nothing specific to the Jackson library) – ADyson Oct 30 '18 at 11:55

2 Answers2

0

I suppose you have your JSON contents as a File object. If you have it as an URL, InputStream, Reader or String, then just modify the code below by taking another readValue method.

With the classes from packages com.fasterxml.jackson.databind and com.fasterxml.jackson.databind.node you can achieve your goal in a straight-forward way. Read the JSON input, find and remove the array element, and write the JSON output:

File file = ...;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);  // for pretty print
JsonNode jsonNode = objectMapper.readValue(file, JsonNode.class);
ArrayNode errorMessages = (ArrayNode) jsonNode.get("errorMessages");
for (int i = 0; i < errorMessages.size(); i++) {
    ObjectNode errorMessage = (ObjectNode) errorMessages.get(i);
    String code = errorMessage.get("code").asText();
    if (code.equals("3114587")) {
        errorMessages.remove(i);  // remove the element
    }
}
objectMapper.writeValue(System.out, jsonNode);
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
-1

delete json[key];

Store your Json object in a variable name json. Find the key that you want to delete and it should work.