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?
I was actually super confused as to why this happens and even more confused about the fact that this confused me. The solution is actually pretty simple. The following lines both work:
Account account = [SELECT Id, Name FROM Account LIMIT 1]; List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 1];
Since the list is directly passed into the serializer it cannot know if you want a list or a single object - therefore throwing the error.