I have this very simple example that serializes and deserializes the first Account found:

System.debug(
    (Account)JSON.deserialize(
    JSON.serialize([SELECT Id, Name FROM Account LIMIT 1]),
    Account.class));

It fails with an error:

System.JSONException: 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

With no own code whatsoever - why does this fail? And what can I do to get it running?

up vote 4 down vote accepted

It's because the query returns a List<Account> unless you specify it somewhere as a single Account record.

Works

Account data = [SELECT Id, Name FROM Account LIMIT 1];
system.debug(JSON.deserialize(JSON.serialize(data), Account.class));

Fails

List<Account> data = [SELECT Id, Name FROM Account LIMIT 1];
system.debug(JSON.deserialize(JSON.serialize(data), Account.class));

The second example above exactly matches the behavior you observed. You could also still one-line it:

system.debug((Account)JSON.deserialize(
    JSON.serialize((Account)[SELECT Name FROM Account LIMIT 1]), // you must cast here
    Account.class
));
  • Wow. I can't believe I missed that. Thanks! Aside from that I noticed other people seem to have similar problems and the exact same error message. Without further investigation I tend to think it has the same error source. Hope this helps. – Semmel Oct 18 '16 at 22:23

The JSON.deserialize is returning a List<Object> not Object. Below Debug is working fine:

System.debug((List<Account>)JSON.deserialize(String.valueOf(JSON.serialize([SELECT Id, Name FROM Account LIMIT 1])), List<Account>.class));

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.