+ Start a Discussion

Save JSON Data into salesforce


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:

Nitin SharmaNitin Sharma
Create a wrapper class and use JSON. Serialize  function in class constructor.
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.