function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shalini Garg 13Shalini Garg 13 

Remove back slash from response

Remove back slash in response json through rest API in workbench post method in salesforce

Eg:- "[{\"Result\":\"No Data Found\",\"RequestType\":\"Delete\",\"RequestID\":\"YPN\"}]"

We want to delete the \ from above json.
Maharajan CMaharajan C
Hi Shalini:

Try like below :
 
String respStr = '"[{\"Result\":\"No Data Found\",\"RequestType\":\"Delete\",\"RequestID\":\"YPN\"}]"';
respStr.replace('\\','');
system.debug('Response --> ' + respStr );

Thanks,
Maharajan.C
Suraj Tripathi 47Suraj Tripathi 47
Hi Shalini Garg 13
through rest API in workbench post please help from that link:-
https://developer.salesforce.com/forums/?id=906F0000000fxruIAA

Please mark it as the best answer if it helps you to fix the issue.
Thank you!
Regards,
Suraj Tripathi
 
Shalini Garg 13Shalini Garg 13
Hi ,

I am getting this error in json response in workbench which i am returning from Apex not getting in debug log Salesforce Apex class
Shalini Garg 13Shalini Garg 13
I am getting the error like below.


Raw Response
HTTP/1.1 200 OK Date: Thu, 20 May 2021 13:38:44 GMT Strict-Transport-Security: max-age=31536000; includeSubDomains X-Robots-Tag: none Cache-Control: no-cache,must-revalidate,max-age=0,no-store,private Set-Cookie: BrowserId=skBb07lwEeuPNAm_YzkTXA; domain=.salesforce.com; path=/; expires=Fri, 20-May-2022 13:38:44 GMT; Max-Age=31536000 Set-Cookie: BrowserId_sec=skBb07lwEeuPNAm_YzkTXA; domain=.salesforce.com; path=/; expires=Fri, 20-May-2022 13:38:44 GMT; Max-Age=31536000; secure; SameSite=None Content-Type: application/json;charset=UTF-8 Vary: Accept-Encoding Content-Encoding: gzip Transfer-Encoding: chunked
"[{\"Result\":\"No Data Found\",\"RequestType\":\"Delete\",\"RequestID\":\"YPNEE5Y7R5S\"}]"
SwethaSwetha (Salesforce Developers) 
HI Shalini,
I'd recommend trying the approach mentioned in https://salesforce.stackexchange.com/questions/213989/jsongenerator-issue

To remove the backslash you have to write the blob value of the JSON String into the body directly in the RestContext. You will have to change the return type of your method from String to void.
@RestResource(urlMapping='/myrestservice/*')
global with sharing class MyRestResource {
    @HttpGet
    global static void doGet() {
        List<Account> accList = [SELECT Id, Name, Phone, Website FROM Account];
        JSONGenerator gen = JSON.createGenerator(false);
        gen.writeStartObject();
        gen.writeObjectField('syncResults', accList);
        gen.writeEndObject();
        String jsonStr = gen.getAsString();
        RestContext.response.addHeader('Content-Type', 'application/json');
        RestContext.response.responseBody = Blob.valueOf(jsonStr);        
    }
}

Also see Related: https://salesforce.stackexchange.com/questions/221062/jsongenerator-writeobjectfield-remove-backslashes

If this information helps, please mark the answer as best. Thank you
Jason Kuzmak 12Jason Kuzmak 12

@swetha 

This worked great for me! Just implemented and all fixed. 

Nagaraju Mogili 52Nagaraju Mogili 52
@Swetha, thanks for the solution.. I have spent hours of time at last the issue was resolved with your solution.