How to Convert sObject to JSON and Vice-Versa Using Apex in Salesforce?
Sample Code:
Account acc =
new
Account(Name =
'SFDC LEGEND'
, Phone =
'9876543210'
, Industry =
'IT'
);
//Code to Convert Account Object to JSON String
String str = JSON.serialize(acc);
system.debug(
'
Convert Account Object to JSON String - '
+ str);
//Code to Convert JSON String to Account Object
Account acc1 = (Account)JSON.deserialize(str, Account.Class);
system.debug(
'Convert JSON String to Account Object - '
+ acc1);
Follow Us