Skip to content

Instantly share code, notes, and snippets.

@ysfdc
ysfdc / ParseJsonApex.class
Last active March 27, 2021 23:24
Simple JSON Parser APEX
public with sharing class parseJson {
public static void parseJson() {
String jsonString = '{"Hogwarts":[{"Houses":[{"Hufflepuff":{"members":2}},{"Gryffindor":{"members":4}},{"Slytherin":{"members":3}},{"Ravenclaw":{"members":2}}]},{"Common Areas":{"Inside":"The Great Hall","Outside":"Hagrids Hut"}}]}';
// Load JSON and get to the root of the document
Map<String, Object> hogwarts = (Map<String,Object>)JSON.deserializeUntyped(jsonString);
// "Hogwarts" is a List
List<Object> hogwartsResults = (List<Object>)hogwarts.get('Hogwarts');
// Houses is the first element in the "Hogwarts" list
Map<String, Object> houses = (Map<String, Object>)hogwartsResults[0];