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
Kenji775Kenji775 

JSON Parsing to list of custom object type error

Hey all.

I am working with the new JSON Parsing functionality, and am hitting a bit of snag trying to parse some JSON into a list of custom objects. The exact error is

 

Internal System Error: 1094975380-22831 (-1000130141)

 

Which looks like there is probably an issue with the JSON parser. I have been able to parse single instaces of an object, but have not been able to do with with a list of them yet. 


Here is what the code looks like.

 

	public class surveyAnswer
	{
		Id contact;
		Integer surveyId;
		String questionText;
		String questionId;
		String questionAnswer;
		
		public surveyAnswer(Id c, Integer s, String qt, String qi, String qa)
		{
			contact = c;
			surveyId = s;
			questionText = qt;
			questionId = qi;
			questionAnswer = qa;			
		}
	}

string jsonString = '[{"questionText":"BLah 1","questionId":"51345X1607X53053","surveyId":51345,"questionAnswer":2.0,"contact":"0034000000UefNI"},{"questionText":"Blah 2","questionId":"51345X1607X53100","surveyId":51345,"questionAnswer":3.0,"contact":"0034000000UefNI"},{"questionText":"blah 3","questionId":"51345X1607X53101","surveyId":51345,"questionAnswer":1.0,"contact":"0034000000UefNI"},{"questionText":"blah 4","questionId":"51345X1607X53103","surveyId":51345,"questionAnswer":1.0,"contact":"0034000000UefNI"}]';
list<surveyAnswer> answers = (List<surveyAnswer>)JSON.deserialize(JSONString, List<surveyAnswer>.class);

 

I thought it might be a problem with the JSON structure, but it validates just fine in JSLint, so it should be fine. This approach should be doable according to the docs at

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_json.htm#apex_methods_system_json 

 

Any help would be much appreciated. Thank you!

Kenji775Kenji775

Just to validate it's not the JSON structure, I tried creating JSON using Salesforce parser (which works, it creates valid JSON) and then attempted to deserailize it got the same error. This is the code I used to attemt my 'round trip' of serializing and deserializing. The 2nd to last line is the one that fails.

 

public class surveyAnswer
{
    Id contact;
    Integer surveyId;
    String questionText;
    String questionId;
    String questionAnswer;
    
    public surveyAnswer(Id c, Integer s, String qt, String qi, String qa)
    {
        contact = c;
        surveyId = s;
        questionText = qt;
        questionId = qi;
        questionAnswer = qa;			
    }
}

list<surveyAnswer> answers = new list <surveyAnswer>();
contact con = [select id from contact limit 1];
surveyAnswer thisAns1 = new surveyAnswer(con.id, 12345, 'blah1','id1','yup');
surveyAnswer thisAns2 = new surveyAnswer(con.id, 12345, 'blah1','id2','nope');

//add the objects
answers.add(thisAns1);
answers.add(thisAns2);

//seraialize them
String JSONString = JSON.serialize(answers);

system.debug(JSONString);

//This dies
list<surveyAnswer> answersObjs = (List<surveyAnswer>)JSON.deserialize(JSONString, List<surveyAnswer>.class);
system.debug(answersObjs);

 

rungerrunger

Hmm, interesting.  I've tried both these examples, and they run fine for me.  My debug output for the second example is:

 

16:17:06.078 (78276000)|USER_DEBUG|[13]|DEBUG|[{"surveyId":12345,"questionText":"blah1","questionId":"id1","questionAnswer":"yup","contact":"003U0000004rmiwIAA"},{"surveyId":12345,"questionText":"blah1","questionId":"id2","questionAnswer":"nope","contact":"003U0000004rmiwIAA"}]

16:17:06.084 (84952000)|USER_DEBUG|[17]|DEBUG|(surveyAnswer:[contact=003U0000004rmiwIAA, questionAnswer=yup, questionId=id1, questionText=blah1, surveyId=12345], surveyAnswer:[contact=003U0000004rmiwIAA, questionAnswer=nope, questionId=id2, questionText=blah1, surveyId=12345])

 

rungerrunger

Ah, I got it to reproduce.  I had been creating the class in the normal way, and then just executing the script below the class in the console.  That works fine.  It's only when the class definition is part of the script itself that it errors out this way.  I'll look into it.

Kenji775Kenji775

Cool, glad I'm not crazy.

If it is just when the class is part of the execute anon that it bugs out, that's not a huge deal. It's just how I was doing my testing so it seemed like it was broken. Would be nice if we could make that work, but if I just know that I can't test it like that, then that's no big deal. I can work around that. Thanks for looking into this for me!

rungerrunger

And thank you for the bug report.  The fix for this should arrive on the production servers in a week or so.

stephanie_zstephanie_z

Hi, Rich:

 

I am running into a similar issue: http://boards.developerforce.com/t5/Apex-Code-Development/Unable-to-use-JSON-deseralize-to-deseralize-an-object-which/td-p/352247

 

Was there any change being introduced for JSON.deserialize? Thanks a lot.

Juan SpagnoliJuan Spagnoli

I'm having the same issue, the problem is with arrays and it doesn't trigger the error until you use the field.

 

Example:

 

public Class MyStatus{
    public site_status {get;get;}
    public List<String> codes {get;set;}
}

String JSONString = '{"site_status":"active","codes":["1","2"]}';
MyStatus myS = (MyStatus)JSON.deserialize(JSONString, MyStatus.class);

System.Debug(myS.site_status);  // <--- This is OK
System.Debug(myS.codes);  // <--- Trigger the internal error

 

 

rungerrunger

Presuming you actually meant the MyStatus class to look like:

public Class MyStatus{
    public String site_status {get;set;}
    public List<String> codes {get;set;}
}

 ...then your example is working for me now.

Juan SpagnoliJuan Spagnoli

Well... it seems they solved it. :D