You need to sign in to do that
Don't have an account?
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:
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(); request.setEndpoint('https://npiregistry.cms.hhs.gov/api/?number='+NPIid); request.setMethod('GET'); 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 ; } }
- 2. Untyped Parsing of JSON Objects
- 3. Parsing JSON Objects in a Type-Safe Manner
http://opfocus.com/json-deserialization-techniques-in-salesforce/- Salesforcce Example: Parse a JSON String and Deserialize It into Objects
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsonparser.htmRegards
Alain
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.
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;}