Publishing Platform Event with JSON in a Text Field?

Question
When attempting to publish a Platform Event with a valid JSON object inside a custom text field (Message__c
), the serialized JSON string appears with backslashes before special characters such as double quotes ("
).
For example, when assigning the following JSON to Message__c
:
{
"attributes": {
"type": "Case",
"url": "/services/data/v57.0/sobjects/Case/Id"
},
"Id": "value",
"RecordTypeId": "value"
}
The resulting event payload contains an escaped string:
{
"ReplayId": "value",
"CreatedDate": "value",
"CreatedById": "value",
"EventUuid": "value",
"Message__c": "{\"attributes\":{\"type\":\"Case\",\"url\":\"/services/data/v57.0/sobjects/Case/Id\"},\"Id\":\"value\",\"RecordTypeId\":\"value\"}",
"_ObjectType": "customEvent__e",
"_EventType": "value"
}
However, the expected payload should retain the JSON structure within Message__c
without escaping double quotes:
"Message__c": {
"attributes": {
"type": "Case",
"url": "/services/data/v57.0/sobjects/Case/Id"
},
"Id": "value",
"RecordTypeId": "value"
}
How can we publish a Platform Event while maintaining the correct JSON format in Message__c
?
Answer
When publishing a Platform Event, JSON objects inside text fields are stored as escaped strings, causing backslashes before special characters like double quotes. This happens because JSON notation requires embedded JSON strings to be properly escaped. To maintain a valid JSON structure, the consuming subscriber must deserialize the string before using it.
Master Salesforce with expert-led training at CRS Info Solutions in Salesforce training in Hyderabad. Gain hands-on experience in Admin, Developer (Apex), and Integration—join a free demo today!!!
If you are publishing the Platform Event using the REST API, the request body itself must be JSON:
{
"ReplayId": "value",
"CreatedDate": "value",
"CreatedById": "value",
"EventUuid": "value",
"Message__c": "someMessage",
"_ObjectType": "customEvent__e",
"_EventType": "value"
}
Explanation: The above JSON represents a Platform Event payload in Salesforce, where ReplayId
, CreatedDate
, CreatedById
, and EventUuid
store metadata about the event. The Message__c
field contains a string (someMessage
), which can be a serialized JSON object. _ObjectType
and _EventType
indicate the event type and structure for processing in Salesforce.
Since Message__c
is a text field, assigning a JSON object directly would result in it being converted to a string. In JSON notation, any embedded JSON string must escape double quotes ("
), which is why you see backslashes (\
).
To work with the correct JSON structure, the consuming Apex subscriber (such as an Apex trigger) should deserialize Message__c
before using it:
Case myCase = (Case) JSON.deserialize(myPlatformEvent.Message__c, Case.class);
If you are processing this event in Apex, you must handle Message__c
as a string and parse it as needed. Salesforce does not store raw JSON objects inside text fields; they are always stored as strings, requiring deserialization when read.
Summing Up
Salesforce stores JSON data in text fields as string literals, automatically escaping double quotes when publishing Platform Events. This behavior is expected because JSON inside a text field is treated as a string rather than an actual JSON object. To maintain proper JSON formatting, subscribers must deserialize the Message__c
field before use. If publishing via the REST API, escaping is required, but Apex consumers can easily convert the stored string back into a usable object using JSON.deserialize()
. Understanding this ensures seamless handling of structured data within Platform Events.
Kickstart Your Salesforce Career in Hyderabad’s Thriving Tech Scene
Hyderabad has rapidly become a powerhouse for IT and cloud computing, with Salesforce at the forefront of digital transformation. Salesforce training in Hyderabad As businesses increasingly adopt Salesforce for CRM, AI, automation, and seamless integrations, the demand for certified professionals is skyrocketing. Industry giants like Deloitte, Accenture, Infosys, TCS, and Wipro are actively hiring skilled Salesforce experts, making specialized training a game-changer for career advancement.
If you’re eager to launch a successful Salesforce career, the right training is crucial. CRS Info Solutions offers comprehensive Salesforce training institutes in Madhapur, covering Admin, Developer (Apex), and Integration modules. With expert-led instruction, hands-on projects, and real-world case studies, this program prepares you to thrive in Hyderabad’s dynamic job market. Take the leap—enroll today and unlock endless opportunities in the Salesforce ecosystem!!!
Related Posts:
Platform Event Flow in Salesforce
The Complete Guide to Salesforce Database Management