Salesforce: QueryResult must start with ‘{‘

I had some logic on Lightning app where some Model record is passed from Apex Controller to Lightning app and then back to Apex Controller.

Today it started failed on the line

JSON.deserialize( jsonPM, Model.class );

with very strange and misleading error

System.JSONException: QueryResult must start with ‘{‘

Looks like there are some problems if you are trying to serialize and deserialize a complex model which consist of record of Object A with related record of Object B type which has related children of Object C type with parents.

{
    "lookupToB__c": "bbb000000-id",
    "lookupToB__r": {
            "Id": "bbb000000-id",
            "Name": "Big B - record 1",
            "children_C__r": [{
                "lookupToB__c": "bbb000000-id",
                "Id": "ccc-----id"
            }, {
                "lookupToB__c": "bbb000000-id",
                "Id": "ccc---1-id"
            }]
        } 
} 

How can I avoid this error, and why is it cropping up when I include both lookupToB__c and lookupToB__r?

Answer

The problem isn’t actually that you specify Parent__r. Rather it is that your Parent__r value specifies Children__r as a List<SObject>.

Serialize a query result which includes a Left Outer Join and look more closely. The child relationship key doesn’t point to a List<SObject>. It points to a Map<String, Object>. That map must specify both totalSize and done in addition to records.

Won’t work:

{
    "Children__r": [{
        "SomeField": "some value"
    }, {
        "SomeField": "some value"
    }]
}

Will work:

{
    "Children__r": {
        "totalSize": 123,
        "done": true,
        "records": [{
            "SomeField": "some value"
        }, {
            "SomeField": "some value"
        }
    }
}

Attribution
Source : Link , Question Author : Patlatus , Answer Author : Adrian Larson

Leave a Comment