Tag Archives: JSON Parsing

JSON Response Parsing in Salesforce Apex

We can use the JSONParser class methods to parse JSON-encoded content. The methods of JSONParser class enable to parse a JSON-formatted response that’s returned from a call to an external service, such as a web service callout.

Here is an example of JSON response parsing using apex.

JSON String Data:

{
"ContactList": [
		{
            "FirstName":"Biswajeet",
            "LastName": "Samal",
			"Email": "test1@test.com",
			"Mobile": "9999999999"
        },
        {
            "FirstName":"Abhijeet",
            "LastName": "Samal",
			"Email": "test2@test.com",
			"Mobile": "8888888888"
        }
    ]
}

Apex Class:

//Wrapper Class For Parsering 
public class JsonParseringClass
{
    //Method To Parse JSON Data
    public ContactList getJSONData()
    {
        ContactList conList = new ContactList();
        //JSON String
        String jsonString = '{"ContactList": [' +
            '{"FirstName":"Biswajeet", "LastName": "Samal", "Email": "test1@test.com", "Mobile": "9999999999"},'+
            '{"FirstName":"Abhijeet", "LastName": "Samal", "Email": "test2@test.com",	"Mobile": "8888888888"}]}';
        //Parse JSON to ContactList
        conList = (ContactList)System.JSON.deserialize(jsonstring, ContactList.class);
        System.debug('Respone- ' + conList);
        return conList;
    }
    
    public class ContactList
    {
        public List<ContactWrapper> ContactList;
    }
    
    public class ContactWrapper
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Mobile { get; set; }
    }
}

Debug Log:

Respone- ContactList:[ContactList=(ContactWrapper:[Email=test1@test.com, FirstName=Biswajeet, LastName=Samal, Mobile=9999999999], ContactWrapper:[Email=test2@test.com, FirstName=Abhijeet, LastName=Samal, Mobile=8888888888])]