function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Poorna DeveloperPoorna Developer 

Store JSON data in salesforce object

Hi Everyone,

I have JSON data. I wish to store this JSON data in my custom object field.
Is this possible in salesforce? Any idea?
Thanks in Advance.
Best Answer chosen by Poorna Developer
Naveen KNNaveen KN
Yes, this is possible. Stamp your JSON format to the respective field and insert the record.

I tried directly saving the JSON data in the console to the text area field. It works!

User-added image

- Naveen K N

All Answers

SwethaSwetha (Salesforce Developers) 
HI Poorna,
I have come across similar posts by others users which might help you

https://salesforce.stackexchange.com/questions/51856/insert-new-records-in-to-custom-object-from-json-string-in-salesforce
https://salesforce.stackexchange.com/questions/56607/how-can-i-deserialze-json-object-and-save-them-to-salesforce
https://salesforce.stackexchange.com/questions/217583/how-to-store-json-data-in-a-salesforce-custom-object
 

Please have a look at this post http://sudipta-deb.blogspot.in/2014/10/json-parsing-with-apex-in-salesforce_17.html

The above link will tell you how to parse and display JSON Data into Tabular format. Now in order to store the same into sObject use below functionality-

public class CountryController {
private List<CountryDataWrapper> countryDataWrapper;

public void parseJSONData(){
    countryDataWrapper = new ParseMultipleJsonData().parse();
    insertCountryInformation();
}

public List<CountryDataWrapper> getCountries(){
    return countryDataWrapper;
}

private void insertCountryInformation(){
    List<Country__c> allCountries = new List<Country__c>();
    for(CountryDataWrapper aCountryDataWrapper : countryDataWrapper){

        allCountries.add(new Country__c(
            Name = aCountryDataWrapper.countryName,
            Capital__c = aCountryDataWrapper.countryCapital,
            Currency__c = aCountryDataWrapper.countryCurrency
        ));
    }
    Database.insert(allCountries);
}
}


If this information helps, please mark the answer as best.Thank you
Malika Pathak 9Malika Pathak 9

Hi poorna,

You need to create a wrapper class to parse this JSON

 

public class NewUser{

       public Double UserId;
       public  Double Id;
       public  String Title;
       public  String Body;

}

and then in your code use it
 
List<NewUser> allUsers = (List<NewUser>)JSON.deserialize(response.getBody(),List<NewUser>.class);
    for( NewUser user: allUsers) {
        NewUsers__c newUser = new NewUsers__c();
        newUser.UserId__c = user.UserId;
        newUser.Id__c= user.Id;
        newUser.Title__c = user.Title;
        newUser.Body__c = user.Body;
        newallUsers.add(newUser);    
    }

If you find this helpful mark it as the best answer.
Poorna DeveloperPoorna Developer
Hi,
I'm created a custom object named - Application__c with the following fields.
Apply_Date--c, Approve_Date_c,Reason_c,Status__c and Data__c.
In the field Data__C i need to store the following JSON data 
{"data":{"expires_at":"2021-04-02T07:34:43Z","redirect_url":"https://www.saltedge.com/dashboard/connect?token=96f20d2a009f87a2f512bf9739bdsdw02e5d5d840ee692ssdewe3w22ws8235326bd0e41e0b8dbac"}}.

Is possible?
 
Naveen KNNaveen KN
Yes, this is possible. Stamp your JSON format to the respective field and insert the record.

I tried directly saving the JSON data in the console to the text area field. It works!

User-added image

- Naveen K N
This was selected as the best answer
Poorna DeveloperPoorna Developer
Hi Naveen,

Can you please share here how you work the above process?
Thanks again.