[SalesForce] System.UnexpectedException: Salesforce System Error with JSON

I am trying to create a JSON array but when I try to run the code I am getting an error

System.UnexpectedException: Salesforce System Error: 884193503-1014 (-1834458735) (-1834458735)

My code :

public void getGenerateJSON(Map<Integer,list<ConfiguredItemsServiceImpl.ImportIdCodeWrapperClass>> ConfiguredItemWrapper){
    JSONGenerator gen = JSON.createGenerator(true);
    System.debug('Configured Item Wrapper ' + ConfiguredItemWrapper);

    for(Integer num : ConfiguredItemWrapper.keySet()){
        for(ConfiguredItemsServiceImpl.ImportIdCodeWrapperClass ConfiguredItemWrapperinfo : ConfiguredItemWrapper.get(num)){
            if(num == 0){
                 gen.writeStartObject();
                    gen.writeFieldName('ProductConfiguration');

                    gen.writeStartObject();
                        gen.writeStringField('Name', ConfiguredItemWrapperinfo.configuredItem.Name);
                        gen.writeStringField('ID', ConfiguredItemWrapperinfo.configuredItem.ID);
                        gen.writeNumberField('Level', ConfiguredItemWrapperinfo.configuredItem.Level__c);
                        if(ConfiguredItemWrapperinfo.configuredItem.Label__c != null)
                            gen.writeStringField('Label', ConfiguredItemWrapperinfo.configuredItem.Label__c);        
                        if(ConfiguredItemWrapperinfo.code != null)
                            gen.writeStringField('code', ConfiguredItemWrapperinfo.code);

                    gen.writeFieldName('ProjectDetails');
                    gen.writeStartObject();
                        gen.writeStringField('Date', String.valueof(date.today()));
                        gen.writeStringField('ID', ConfiguredItemWrapperinfo.configuredItem.Basic_Config_Info__c);      
                        //gen.writeStringField('Name', ConfiguredItemWrapperinfo.configuredItem.Basic_Config_Info__r.Name);
                        gen.writeStringField('Name', ConfiguredItemWrapperinfo.configuredItem.Name);

                        gen.writeFieldName('Output');
                        gen.writeStartObject();
                            gen.writeStringField('FolderPath', 'XYZ Path');
                            gen.writeStringField('FileName', 'XYZ NAME');
                        gen.writeEndObject();
                    gen.writeEndObject();





                jsonString += gen.getAsString();
            }else if(num == 1){
                gen.writeStartObject();
                    gen.writeFieldName('Assembly');
                    gen.writeStartArray();
                        gen.writeStartObject();
                            gen.writeStringField('ModelCode', 'Model code 123456');
                            gen.writeStringField('Description', 'Description 123456');
                            //System.debug('Map information in assembly' +ConfiguredItemWrapper.get(num));
                        gen.writeEndObject();
                    gen.writeEndArray();
                gen.writeEndObject();
                jsonString += gen.getAsString();
            }

        }
    }  

    gen.writeEndObject();
    gen.writeEndObject();
    jsonString += gen.getAsString();
    System.debug('JSON Result in string ' + jsonString);
}

When the same code by commenting the Array methods it works. But issue stands when I try to convert it into array. Do anyone have any solution on this issue. The issue is at line no 52 just below else if(num == 1) on gen.writeStartObject(); Do I need to log a case with salesforce. Is this issue from background. As mention here Please guide me how can I fix this issue.

Best Answer

Because you are adding key value pairs inside the array, you should start and end object..See the below code:

Check this link on how arrays are defined in JSON : http://www.objgen.com/json?demo=true

String jsonString = '';
JSONGenerator gen = JSON.createGenerator(true);
for(Account accObj : [SELECT Id,Name,Phone FROM Account WHERE Id = '001Z023456ztasN' LIMIT 1]){
    gen.writeStartObject();
    gen.writeFieldName('attendees');

    gen.writeStartArray();

    gen.writeStartObject();
    gen.writeStringField('Name', accObj.Name);
    gen.writeStringField('ID', accObj.ID);
    //gen.writeNumberField('Level', accObj.Phone);
    gen.writeEndObject();

    gen.writeEndArray();
    gen.writeEndObject();
    jsonString += gen.getAsString();
}

System.debug('JSON Result in string ' + jsonString);

JSON Result in string { "attendees" : [ { "Name" : "test lead Company", "ID" : "001Z0123450ztasNIAQ" } ] }

Related Topic