The community forums are in BETA and closed to the public.

Why is an escape character appeares in JSON stringified message

When I send a message like as follows, I receive a message like this

  • String (first pattern) comes with escape character: { \"JSON stringify\": \"hey\" }
  • JSON object (the second pattern) comes without escape character: { "JSON stringify": "hey" }

What makes this difference? The escape character is annoying. Should I always send JSON object?

str = JSON.stringify({ "JSON stringify": "hey" })
obj = { "JSON object": "hey" }

function publish() {
           channel: 'demo',
            message: str
            channel: 'demo',
            message: obj

I use the following SDKs.

  • Java SDK 3.7.2
  • JS SDK 3.7.2

Please look at this code.


We always need escape character to send string! sorry!

[NOTE: I moved your question from the Knowledge Base to here, the Dev Community. The permissions to post to the KB was an oversight.]

Always send JSONObject. Do not send JSON as a String. PubNub SDK will always escape and unescape for you.

See this KB article for more details:

Is your "string" actually JSON content?

Look at the code,

OK, then. Yes, that is JSON - don't stringify the JSON. Send as JSONObject as specified in the KB article I referred you to. Cheers.

Comments to this discussion are now closed!