Tag Archives: Json

Convert sObject to JSON String and JSON String to sObject Using Apex in Salesforce

Sample Code:

Account acc = new Account(Name = 'Account Name', Phone = '8888888888', Industry = 'Agriculture');
//Code to convert Account to JSON string
String str = JSON.serialize(acc);
system.debug('Account JSON Data - ' + strJSON);
//Code to convert JSON string to Account
Account acc1 = (Account)JSON.deserialize(str, Account.Class);
system.debug('Account Data - ' + acc1);

Output:

Generate Json Data Using Apex in Salesforce

Child Class:

public class Child{

    public String Name;
    
    public Child(String name) {
        this.Name = name;
    }
}

Parent Class:

public class Parent{

    public Child[] ChildRecords;
    public String Name;
    
    public Parent(String Name) {
        this.Name = name;
        ChildRecords = new Child[0];
    }
}

Pass parameters to above class using below code:

//Select Account Id from Account Object
String accountId = '0012800001A4UgG';

//Select Account & Contact Records
Account acc = [SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account WHERE Id =: accountId];

//Create a parent record
Parent par = new Parent(acc.Name);

//Loop on Contacts
for(Contact con: acc.Contacts) {
    par.childRecords.add(new Child(con.Name));
}

//Get the Json data
String jsonData = JSON.serialize(par);
System.debug('JsonData-' + jsonData);

Here is the debug log snapshot of Json data:
Json Data

Note: This is the sample class. You can modify it as per your requirement.

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])]