I'm trying to convert a JSON file, which has information from one or more objects. I saw that one way was to transform the JSON String to JSONParser and loop to generate the objects.
I want to convert these objects to SObject to do the proper treatment instead of converting straight to the specific object, such as Account. The problem is that an error occurs at conversion time:
"Exception: System.JSONException: Nested object for polymorphic foreign key must have a field attributes before any other fields.".
Please, anyone have an idea how I could do this conversion?
String jsonText = '[{"Id":"0015800001PTKzHAAX","Name":"test1"},{"Id":"0015800001PTKxpAAH","Name":"test2"}]';
JSONParser parser = JSON.createParser(jsonText);
system.debug('JSONParser: ' + parser);
while (parser.nextToken() != null)
{
if (parser.getCurrentToken() == JSONToken.START_ARRAY)
{
while (parser.nextToken() != null)
{
if (parser.getCurrentToken() == JSONToken.START_OBJECT)
{
SObject obj = (SObject)parser.readValueAs(SObject.class);
system.debug('Object: ' + obj);
}
}
}
}