Access JSON Object Properties in Apex

Sometimes we need to parse simple json objects in apex. for that, we need to create a new wrapper class with those properties and deserialize the response object in apex. but here is the simple code to access deserialize Untyped json object properties in apex. 

Accessing Object properties in Apex

String response = '[{"code": "400", "title": "Bad request", "message": "The request is invalid or not properly formed"}]';
List<Object> responseErrors = (List<Object>)JSON.deserializeUntyped(response);

for(Object errorObj: responseErrors){
	Map<String, Object> errorMap = (Map<String, Object>)errorObj;
	system.debug(errorMap.get('code')); // 400
	system.debug(errorMap.get('title')); // Bad request
	system.debug(errorMap.get('message')); // The request is invalid or not properly formed
}
Labels:
Join the conversation