I have a Map<string, String>
that is populated using the following code below. When I attempt to convert this into a serialized string so that it can be passed into Blob.valueOf
, the \"
(backslashes) are added to the values in the map. We want to remove those values in the map.
How can I pass the serialized string so that there are no backslashes?
Code:
public class MyWrapper {
public String firstName;
public String lastName;
}
Map<String, String> mapToSerialize = new Map<String, String>();
list<Contact> contactList = [Select Id, FirstName, LastName from Contact];
for(Contact cont : contactList) {
MyWrapper contWrapper = new MyWrapper();
contWrapper.firstName = cont.FirstName;
contWrapper.lastName = cont.LastName;
mapToSerialize.put(cont.Id, JSON.serialize(contWrapper));
}
response.responseBody = Blob.valueOf(JSON.serialize(mapToSerialize));
Current Output:
{
"123123" : { \"FirstName\" : \"First Name\", \"LastName\" : \"the last name\" }
}
Desired output:
{
"123123" : { "FirstName" : "First Name", "LastName" : "the last name" }
}
What I tried, but didn't work
- added
response.addHeader('Content-Type', 'application/json')
but that returned the same output asCurrent Output