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

Retrieve All Field Names From JSON Object Or Object Node

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());
}

Leave a Reply

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