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

Retrieve all key-value pair from JSON Object or ObjectNode

Category: Rest Assured

We can use fields() method to get all fields (with both names and values) of a JSON Object. It returns an Iterator<Entry>.

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 key-value pair
Iterator<Entry> allFieldsAndValues = employeeInfo.fields();
System.out.println("All fields and their values are : ");
while(allFieldsAndValues.hasNext())
{
	Entry node = allFieldsAndValues.next();
	System.out.println("Key is : "+node.getKey()+" and its value is : "+node.getValue());
}

Leave a Reply

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