Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

Remove a field from JSON Object or ObjectNode

Category: Rest Assured

Use remove(String fieldName) method to remove a field from ObjectNode. It will return value of the field, if such field existed; null if not.

Example Code for Json Object:

{
       "firstname" : "Phani",
       "lastname" : "Nagula"
}
// Create an object to ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
		
// Creating Node that maps to JSON Object structures in JSON content
ObjectNode employeeInfo = objectMapper.createObjectNode();

employeeInfo.put("firstname", "Phani");
employeeInfo.put("lastname", "Nagula");


// To remove a field
String removedFieldValue = employeeInfo.remove("firstname").asText();
System.out.println("Value of Removed field is " + removedFieldValue);
String removedJsonObject = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeeInfo);
System.out.println("After removing field , JSON Object is : \n"+ removedJsonObject);

Leave a Reply

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