Category:
Rest Assured
To retrieve all field names from a ObjectNode, we need to use fieldNames() methods which returns an Iterator. To get count of fields in an ObjectNode, we can use size() method.
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 get all field names
System.out.println("Count of fields in ObjectNode : "+ employeeInfo.size());
Iterator allFieldNames = employeeInfo.fieldNames();
System.out.println("Fields are : ");
while(allFieldNames.hasNext())
{
System.out.println(allFieldNames.next());
}