+ Start a Discussion
DevSFDevSF 

Save JSON Data into salesforce

Hi,

I have got JSON response from an exernal system and it consists of some data that needs to be saved in salesforce.
Using some tool i have deserialized the JSON data into apex class.
and how do i save that data to salesforce. I mean it needs to be saved in Account and Contact SOjects in salesforce.

How to do This...!
Tolga SunarTolga Sunar
The following code snippet should be quite self explanatory for your issue:
 
res= http.send(req);

Map<String, Object> message = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
                    
String JSON_Value_1 = (String)message.get('JSON_Key_1');
String JSON_Value_2 = (String)message.get('JSON_Key_2');
String JSON_Value_3 = (String)message.get('JSON_Key_3');
String JSON_Value_4 = (String)message.get('JSON_Key_4');
String JSON_Value_5 = (String)message.get('JSON_Key_5');
String JSON_Value_6 = (String)message.get('JSON_Key_6');
String JSON_Value_7 = (String)message.get('JSON_Key_7');                 

Your_Object__c YourObjectRecord = [SELECT Id FROM Your_Object__c WHERE Id='SampleId'];                  
                    
YourObjectRecord.Your_Field_1__c = JSON_Value_1;
YourObjectRecord.Your_Field_2__c = JSON_Value_2;
YourObjectRecord.Your_Field_3__c = DATE.valueOf(JSON_Value_3);
YourObjectRecord.Your_Field_4__c = JSON_Value_4;
YourObjectRecord.Your_Field_5__c = JSON_Value_5;
YourObjectRecord.Your_Field_6__c = JSON_Value_6;
YourObjectRecord.Your_Field_7__c = JSON_Value_7;
                
update YourObjectRecord;

Where we assume the response JSON is as follows:
 
{
          "JSON_Key_1":"JSON_Value_1",
          "JSON_Key_2":"JSON_Value_2", 
          "JSON_Key_3":"JSON_Value_3", 
          "JSON_Key_4":"JSON_Value_4",
          "JSON_Key_5":"JSON_Value_5",
          "JSON_Key_6":"JSON_Value_6",
          "JSON_Key_7":"JSON_Value_7"          
}

Nitin SharmaNitin Sharma
Create a wrapper class and use JSON. Serialize  function in class constructor.
DevSFDevSF
Thank You Nitin & Sunar

Can you give sample code related to my scenario@ Nitin Sharma.

It would be very helpful if you do that for me...!
Nitin SharmaNitin Sharma
Go through these links it will help you.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm
https://developer.salesforce.com/page/Getting_Started_with_Apex_JSON

Thanks,

Nitin