Last active
December 23, 2021 19:45
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 protected]"}'); | |
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 protected]"},{"id":"002","email":"[email protected]"}]'); | |
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