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
NanduNandu 

Json Deserialization failed on token 'null' and has left off in the middle of parsing a row. Will go to end of row to begin parsing the next row

String jsonText = '{"Data": [{"attributes": {"type": "Contact","url":"/services/data/v35.0/sobjects/Contact/0036100000JUXKkAAP"},"Email":"positi@gmail.com","Description": "","Phone":"9999999","LastName": "sasumi"},{"attributes": {"type": "Contact","url":"/services/data/v35.0/sobjects/Contact/0036100000JUXKkAAP"},"Email":"edge@gmail.com","Description": "","Phone":"88888","LastName": "pavanTL"}]}';

Map<String, Object> deserialized = (Map<String, Object>)JSON.deserializeUntyped(jsonText);
List<Map<String, Object>> data = new List<Map<String, Object>>();
for (Object instance : (List<Object>)deserialized.get('Data')){
    data.add((Map<String, Object>)instance); 
}

String dataSerialize=JSON.serialize(data);
System.debug('dataSerialize...'+dataSerialize);
try
{
   SObject customObject = (SObject)JSON.deserialize(dataSerialize, Sobject.class);
    Database.upsert(customObject);
    System.debug(' customObject...: '+customObject);
}
catch(Exception ex)
{
    System.debug(' @@@@@ Don\'t visible '+ex.getMessage());
}

 
Best Answer chosen by Nandu
Sampath SuranjiSampath Suranji
Hi,
Try below code.
String jsonText = '{"Data": [{"attributes": {"type": "Contact","url":"/services/data/v35.0/sobjects/Contact/0036100000JUXKkAAP"},"Email":"positi@gmail.com","Description": "","Phone":"9999999","LastName": "sasumi"},{"attributes": {"type": "Contact","url":"/services/data/v35.0/sobjects/Contact/0036100000JUXKkAAP"},"Email":"edge@gmail.com","Description": "","Phone":"88888","LastName": "pavanTL"}]}';

Map<String, Object> deserialized = (Map<String, Object>)JSON.deserializeUntyped(jsonText);
List<string > finalJsonString = new List<string>();
List<Map<String, Object>> data = new List<Map<String, Object>>();
for (Object instance : (List<Object>)deserialized.get('Data')){
    finalJsonString.add(JSON.serialize(instance)); 
}

try
{
    for(string s: finalJsonString){
        SObject customObject = (SObject)JSON.deserialize(s, Sobject.class);
        System.debug('customObject '+ customObject);
        Database.upsert(customObject);
        System.debug(' customObject...: '+customObject);
    }
    
}
catch(Exception ex)
{
    System.debug(' @@@@@ Don\'t visible '+ex.getMessage());
}

regards

All Answers

Sampath SuranjiSampath Suranji
Hi,
Try below code.
String jsonText = '{"Data": [{"attributes": {"type": "Contact","url":"/services/data/v35.0/sobjects/Contact/0036100000JUXKkAAP"},"Email":"positi@gmail.com","Description": "","Phone":"9999999","LastName": "sasumi"},{"attributes": {"type": "Contact","url":"/services/data/v35.0/sobjects/Contact/0036100000JUXKkAAP"},"Email":"edge@gmail.com","Description": "","Phone":"88888","LastName": "pavanTL"}]}';

Map<String, Object> deserialized = (Map<String, Object>)JSON.deserializeUntyped(jsonText);
List<string > finalJsonString = new List<string>();
List<Map<String, Object>> data = new List<Map<String, Object>>();
for (Object instance : (List<Object>)deserialized.get('Data')){
    finalJsonString.add(JSON.serialize(instance)); 
}

try
{
    for(string s: finalJsonString){
        SObject customObject = (SObject)JSON.deserialize(s, Sobject.class);
        System.debug('customObject '+ customObject);
        Database.upsert(customObject);
        System.debug(' customObject...: '+customObject);
    }
    
}
catch(Exception ex)
{
    System.debug(' @@@@@ Don\'t visible '+ex.getMessage());
}

regards
This was selected as the best answer
NanduNandu
List<SObject> dataFields=new List<SObject>();
    for(string s: finalJsonString){
        SObject customObject = (SObject)JSON.deserialize(s, Sobject.class);
        dataFields.add(customObject);
        //Database.upsert(customObject);
        System.debug(' customObject...: '+customObject);
    }
    Database.upsert(dataFields);
when i was try to write DML operation outsaid of the for loop  It's throwing error like...
DML on generic List<SObject> only allowed for insert, update or delete
 
Sampath SuranjiSampath Suranji
Hi,

That is a limitation in the platform. it is better if you can provide the Id attribute also inside the json string and try with update.
String jsonText = '{"Data": [{"attributes": {"type": "Contact","url":"/services/data/v35.0/sobjects/Contact/0036100000JUXKkAAP"}, "Id":"0036100000JUXKkAAP", "Email":"positi@gmail.com","Description": "","Phone":"9999999","LastName": "sasumi"},{"attributes": {"type": "Contact","url":"/services/data/v35.0/sobjects/Contact/0036100000JUXKkAAP"}, "Id" : "0036100000JUXKkAAP", "Email":"edge@gmail.com","Description": "","Phone":"88888","LastName": "pavanTL"}]}';

then,
---------------------------
 update(dataFields);

regards
NanduNandu

Thank you @Sampath Suranji
Miriam Elizabeh Urgiles hurtadoMiriam Elizabeh Urgiles hurtado


How can it be when it is inserted ?, I have tried to create several records but I get the following error:


[
    {
        "message": "Json Deserialization failed on token 'null' and has left off in the middle of parsing a row. Will go to end of row to begin parsing the next row",
        "errorCode": "INVALID_FIELD"
    }
]


In postman
[{"Codigo__c":403.0,"Contrato__r":{"Id_contrato_JDE__c":200870468.0},"Costo__c":0.00,"Descripcion__c":"TOP-1 PINTURA ELASTOMERICA EN FACHADAS.- BLANCO BLANCO/REPOSE GRAY/DAPPER TAN ","Eliminado__c":false,"Enviado_JDE__c":false,"Fecha_de_creacion__c":"2020-05-01T18:39:09","Financiamiento__c":"a0G1h000002fKRzEAM","GrupoId__c":2.0,"Monto__c":0.00,"Name":null,"Nombre_del_Acabado__c":"TOP-1 PINTURA ELASTOMERICA EN FACHADAS.- BLANCO BLANCO/REPOSE GRAY/DAPPER TAN ","Nombre_Grupo__c":"","Promocion__c":false,"Descuento__c":false,"Cotizacion__c":"a0B1h0000025dI0EAI","Numero_de_Cita__c":null},{"Codigo__c":404.0,"Contrato__r":{"Id_contrato_JDE__c":200870468.0},"Costo__c":0.0,"Descripcion__c":null,"Eliminado__c":true,"Enviado_JDE__c":false,"Fecha_de_creacion__c":"2020-05-01T18:39:09","Financiamiento__c":"a0G1h000002fKRzEAM","GrupoId__c":2.0,"Monto__c":0.0,"Name":null,"Nombre_del_Acabado__c":"PINTURA DE FACHADAS- TOP-1 - APLICACIÓN DE PINTURA A CRITERIO DEL CONSTRUCTOR","Nombre_Grupo__c":null,"Promocion__c":false,"Descuento__c":false,"Cotizacion__c":"a0B1h0000025dI0EAI","Numero_de_Cita__c":null}]


when I send a record at the same time it does not give an error but when I send several records it sends the error message