2

I am trying to remove "\" from the JSON below:

I have tried replace,replaceall functions but didn't work.

.replaceall('\n','');
.replace('\'', '');
.replaceAll("\\\\", "");

Input:

"[{\"stage\":\"10 - Application\",\"oppId\":\"006234324JymAAE\",\"message\":\"Opportunity status created.\"},{\"stage\":null,\"oppId\":\"00653434427nhCBAAY\",\"message\":\"Opportunity not found or permission denied.\"}]"

Output :(a Valid JSON)

"[{"stage":"10 - Application","oppId":"006220000034JymAAE","message":"Opportunity status created."},{"stage":null,"oppId":"00654000007nhCBAAY","message":"Opportunity not found or permission denied."}]"

I am looking for a generic function that can remove the "\" and Spaces if any.

Code:

@RestResource(urlMapping='/oppQuery/*') 
global class oppStage 
{
    @HttpPost
    global static String doPost(list<String> opportunityIds)
    {
        list<oppResult> results = new list<oppResult>();
        map<Id, Opportunity> oppsById = new map<Id,Opportunity>([SELECT Id, StageName,  FROM Opportunity WHERE Id in: opportunityIds]);
        for(String s : opportunityIds)
        {
            Opportunity o = oppsById.get(s);
            if(o == NULL || o.StageName != 'Won')
            {
                oppResult result = new oppResult(s, NULL, 'Opportunity not found or permission denied.');
                results.add(result);
            }
        }
        String JSONString = JSON.serialize(results); //convert the List of results in to Json
        return decodedJson; 
    }

}


public class oppResult
    {
        public String oppId{get; set;}
        public String stage{get; set;}
        public String message{get; set;}


        public FResult(Id pOppId, String pStage, String pMessage)
        {
            oppId = pOppId;
            stage = pStage;
            message = pMessage;
        }

    }
  • have you tried using the JSON Parser? – glls Jul 11 '19 at 23:24
  • I cannot use the JSON Parser in here. This is a post call (REST)where I am trying to query the records with a given opportunity ID List and send the records in the form of JSON by serializing the List of records .while i am sending I am having the issue with '\' . I would like to remove it. – Donald Jul 11 '19 at 23:27
1

You've "double-encoded" your JSON. To get back to "normal" JSON, just deserialize it.


// Note: \\ becomes \ when the compiler converts this to a string
String source = '"[{\\"stage\\":\\"10 - Application\\",\\"oppId\\":\\"006234324JymAAE\\",\\"message\\":\\"Opportunity status created.\\"},{\\"stage\\":null,\\"oppId\\":\\"00653434427nhCBAAY\\",\\"message\\":\\"Opportunity not found or permission denied.\\"}]"';
String decodedJson = (String)JSON.deserialize(source, String.class);

Output

[{"stage":"10 - Application","oppId":"006234324JymAAE","message":"Opportunity status created."},{"stage":null,"oppId":"00653434427nhCBAAY","message":"Opportunity not found or permission denied."}]


It may be that you should not be encoding the data into a JSON to begin with, since it already is.


Based on edit:

Apex automatically serializes the result for you. This is what's causing the double-encoding. To fix this, change this code:

global static list<oppResult> doPost(list<String> opportunityIds)
    // ... //
    return results;
}
  • Thanks for the Input Sfdxfox, list<StringHelper> results = new list<String>(); //Converting the list of records which are obtained by SOQL Query. String JSONString = JSON.serialize(results); // passing the List to get the records in Json format String decodedJson = (String)JSON.deserialize(results, String.class); return decodedJson; .. I have the issue in the o/p as now "[" it is giving this response – Donald Jul 12 '19 at 1:24
  • @Donald You should not be serializing or deserializing, as far as I can tell. Your problems start with String JSONString = JSON.serialize(results);, which I suspect is already a JSON string. We'd have to see more code to get the full picture, though. – sfdcfox Jul 12 '19 at 1:30
  • Please see the edited post sfdcfox...Thanks for helping me out – Donald Jul 12 '19 at 1:36
  • @Donald The problem is in your method. Edited to show the fix. – sfdcfox Jul 12 '19 at 1:57
0

If you literally have this string as input:

"[{\"stage\":\"10 - Application\",\"oppId\":\"006234324JymAAE\",\"message\":\"Opportunity status created.\"},{\"stage\":null,\"oppId\":\"00653434427nhCBAAY\",\"message\":\"Opportunity not found or permission denied.\"}]"

Then valid (useful) JSON would look like this: (no surrounding double quotes)

[{"stage":"10 - Application","oppId":"006234324JymAAE","message":"Opportunity status created."},{"stage":null,"oppId":"00653434427nhCBAAY","message":"Opportunity not found or permission denied."}]

and to go from input to result would be:

String s = '"[{\"stage\":\"10 - Application\",\"oppId\":\"006234324JymAAE\",\"message\":\"Opportunity status created.\"},{\"stage\":null,\"oppId\":\"00653434427nhCBAAY\",\"message\":\"Opportunity not found or permission denied.\"}]"';

String t = s.replace('\\\\','').substringAfter('"').substringBeforeLAst('"');
system.debug(t);

USER_DEBUG|[4]|DEBUG|[{"stage":"10 - Application","oppId":"006234324JymAAE","message":"Opportunity status created."},{"stage":null,"oppId":"00653434427nhCBAAY","message":"Opportunity not found or permission denied."}]

or more succinctly and reliable (avoiding dealing with embedded \ as legit characters in the json property values):

String s = '"[{\"stage\":\"10 - Application\",\"oppId\":\"006234324JymAAE\",\"message\":\"Opportunity status created.\"},{\"stage\":null,\"oppId\":\"00653434427nhCBAAY\",\"message\":\"Opportunity not found or permission denied.\"}]"';    
String t = (String) Json.deserialize(s.substringAfter('"').substringBeforeLast('"'),String.class);
System.debug(t);

USER_DEBUG|[4]|DEBUG|[{"stage":"10 - Application","oppId":"006234324JymAAE","message":"Opportunity status created."},{"stage":null,"oppId":"00653434427nhCBAAY","message":"Opportunity not found or permission denied."}]

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.