0

I'm returning one JSON response from the rest API from another Salesforce instance. while deserialising JSON I'm getting System.JSONException: Malformed JSON: Expected '{' at the beginning of Map

Returning Map code:

Map<Id,Boolean> activeAccPerIdMap = new Map<Id,Boolean>();
string activeAccPerIdMapJSON = JSON.serializePretty(activeAccPerIdMap);
return activeAccPerIdMapJSON;

In the instance from where it is called:

system.debug('>>> RESPONSE_BODY... '+res1.getbody());
        
string jsonBody = res1.getbody();
system.debug('>>> jsonBody...'+JSON.deserialize(jsonBody,Map<string,Boolean>.class));

I'm receiving the returned value in this form: {\n \"0012O00000E5MSOQA3\" : false\n}

How can I deserialize the Map in my method from where I'm calling

2
  • 4
    What is the actual value of the JSON body you are receiving?
    – David Reed
    Jun 13 '21 at 0:15
  • I'm getting value in this form: {\n \"0012O00000E5MSOQA3\" : false\n}
    – sfdc
    Jun 13 '21 at 14:26
2

Looks like you have an extra level of encoding. The valid JSON would be:

{
    "0012O00000E5MSOQA3": false
}

Note the lack of a backslashes escaping the double quotes. But if that goes through a second serialize the result will just be a string (with the quotes escaped).

A common cause of this double serialization is to serialize yourself in a method body and then return that JSON string via a method that automatically serializes. For that case, return the Map from the method and let the platform do the serialization.

PS

For some reason that I don't know returning Map<Id, String> from a @HttpPost method is not supported. Making that map Map<String, String> instead works around that.

2
  • When I try to return the Map from the method, I'm getting this error HttpPost methods do not support return type of Map<Id,Boolean>
    – sfdc
    Jun 14 '21 at 0:10
  • @sfdc See the PS.
    – Keith C
    Jun 14 '21 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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