Skip to content

Instantly share code, notes, and snippets.

@the-dvlpr
Last active December 23, 2021 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-dvlpr/ce1dcee81f64cdc8b2a8b9e9dc9446ca to your computer and use it in GitHub Desktop.
Save the-dvlpr/ce1dcee81f64cdc8b2a8b9e9dc9446ca to your computer and use it in GitHub Desktop.
Getting values out of a JSON list of objects with Apex code.
// JSON DESERIALIZATION (API RESPONSE OR OTHER)
// --------------------
// Deserialize and cast JSON string into single object
Map<String,Object> jsonObjectMap = (Map<String,Object>)JSON.deserializeUntyped('{"id":"001","email":"email@email.com"}');
String externalId = String.valueOf(jsonObjectMap.get('id'));
// --------------------
// Deserialize and cast JSON string into a list of objects
List<Object> jsonObjectsList = (List<Object>)JSON.deserializeUntyped('[{"id":"001","email":"email@email.com"},{"id":"002","email":"email2@email.com"}]');
for(Object jsonObjectString : jsonObjectsList){
// Cast to JSON string to Apex map
Map<String,Object> jsonObjectMap = (Map<String,Object>)jsonObjectString;
System.debug(String.valueOf(jsonObjectMap.get('id')));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment