How to convert sObject to JSON String and JSON string to sObject using apex in Salesforce?

Sample Code:

Contact con = new Contact(FirstName = 'First', LastName = 'Last', Phone = '9999999999', Email = 'Test@test.com');
/* Code to convert Contact to JSON string */
String strJSON = JSON.serialize(con);
system.debug('JSON String is ' + strJSON);
/* Code to convert JSON string to Contact */
Contact con1 = (Contact)JSON.deserialize(strJSON, Contact.Class);
system.debug('Contact is ' + con1);

Output:


Cheers!!!

No comments:

Post a Comment