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
Jonathan Osgood 3Jonathan Osgood 3 

Parse JSON into custom object

Hi All,

I could use some guidance on how to properly parse my JSON into a custom object. I am able to get all of my deserialized results in the debug log, but I am not able to access individual variables to create a new object and populate the fields. I can get top level object results, but not individual member data like myMap.get('specificVariable');

Full code:
public class NPIcalloutHelper {
    public static String getDocById(Integer NPIid){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        HttpResponse response = http.send(request);
        String strResp = '';
           system.debug('******response '+response.getStatusCode());
           system.debug('******response '+response.getBody());
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) 
            // Deserializes the JSON string into collections of primitive data types.
           Map<String, Object> npiResults = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            // Cast the values in the 'results' key as a list
            List<Object> allresults =(List<Object>)npiResults.get('results');
            System.debug('allresults' + allresults );
        return strResp ;


Alain CabonAlain Cabon
Hi Jonathan,
  • 2. Untyped Parsing of JSON Objects 
  • 3. Parsing JSON Objects in a Type-Safe Manner
  • Salesforcce Example: Parse a JSON String and Deserialize It into Objects​

Timo BierbrauerTimo Bierbrauer
For future readers: Alain's links only show parsing into Objects, not sObjects or custom Objects as asked. I couldn't find a way to do that because your JSON will never happen to have '__c' attached to the field names, so it will never match your custom class. 
Only method that worked for me was to take the json (in my case from a REST callout response) and do
String jsonBody = response.getBody().replaceAll('":','__c":');
on it. If you have attributes in there like a leading "data", that holds your list of records you have to undo the replace and create a class, that has a variable 'data' holding a list of your Sobject.

Then deserialize typed.
Sandeep Kumar GondSandeep Kumar Gond
Let's assume your you want to Parse your JSON String to fund__c object and your string is like this for one record
fields = { "Name":"Fund1", ...
Just add attributes to your string like this
fields = {"attributes": {"type": "fund__c"}, "Name":"Fund1", ...
Then Parse you String in Apex
fund__c  sa = new fund__c();
sa = (fund__c )JSON.deserialize(fields,Sobject.class);
If your string is for more than one records then make your JSON String like this
fields = "records":[{"attributes": {"type": "fund__c"}, "Name":"Fund1", ...
List<fund__c>  sa = new List<fund__c>();
sa = (List<fund__c> )JSON.deserialize(fields,Sobject.class);
so if the system that generates the JSON can provide that, get it added.
Or if the records are always fund__c:
public List<fund__c> records {get; set;}