+ Start a Discussion
Gautam_KasukhelaGautam_Kasukhela 

Platform Events: JSONGenerator adding \n & \r

Hello All,
    I have a need to send a platform event with the contents of an object put in a structured JSON format. Includes a few JSON arrays. 
For this, I used JSONGenerator, to structure my code:
//Sample code 

JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('summary', this.summary);
gen.writeStringField('location', this.location);
gen.writeFieldName('start');
gen.writeStartObject();        
 
......


and I followed the below article to structure the JSON

https://developer.salesforce.com/index.php?title=Getting_Started_with_Apex_JSON

Now, I get the generator as a String and set it to a variable and subsequently assign it to a platform event's Text field.
String ganDetailsJSON = generator.getAsString();

The problem is the receiver of this platform event gets content with all \n. \r & \\ characters :
,\\r\\n \\\“accountNumber\\\” : \\\“88209\\\“,\\r\\n \\\“accountName\\\” : \\\“Harry Potter\\\“,\\r\\n \\\“adviserCode\\\” : \\\“14\\\“,\\r\\n \\\“branchCode\\\” : \\\“99\\\“,\\r\\n \\\“designation\\\”

How can I get rid of the additional characters that are added and make it more readable and parsable JSON format? 
If it was over a webservice, then converting it to a Blob would have helped. But given its a platform event and I need to set it in a tecxt field, is there any other way to work it out?

Regards,
Gautam