PubSub JSON parsing difficulty

Hello. I’m using a simple javascript command to log a response from the Twitch PubSub server via websocket, as described in the documentation.

The following is a specific example (with client ID’s X’d out) of what the data returned.

{“type”:“MESSAGE”,“data”:{“topic”:“channel-bitsevents.XXXXX”,“message”:"{“user_name”:“metal_babble”,“channel_name”:“cleartonic”,“user_id”:“XXXXXX”,“channel_id”:“XXXXXX”,“time”:“2016-09-18T13:54:13.668Z”,“chat_message”:“cheer1 Test 2.”,“bits_used”:1,“total_bits_used”:2,“context”:“cheer”}"}}

I’m getting errors when I try to parse ( JSON.parse ) this data. I simply checked against to see if it was erroneous, and I believe it is. Look at the message data, it’s all messed up and doesn’t close properly. And there’s "/"s everywhere?

I have no problem using the Twitch API for followers/subscribers with parsing JSON data, or receiving blank responses (error:"") when listening to PubSub, But receiving a legitimate response for a Bit response returns this error.

Thank you

The message is a JSON encoded string, so you have to parse that part as JSON again. What error are you getting? If you simply can’t access what’s in the message, then that’s your solution.

Admittedly I am new to this side of scripting. The console just returns the error “Uncaught SyntaxError: Unexpected token u in JSON at position 77”

Here’s the very basic javascript code I’m trying to read the data with, it’s trying to parse specifically the message I received in the console before:


function testjson () {

var obj = $.parseJSON(’{“type”:“MESSAGE”,“data”:{“topic”:“channel-bitsevents.XXXX”,“message”:"{“user_name”:“metal_babble”,“channel_name”:“cleartonic”,“user_id”:“XXXX”,“channel_id”:“XXXX”,“time”:“2016-09-18T13:54:13.668Z”,“chat_message”:“cheer1 Test 2.”,“bits_used”:1,“total_bits_used”:2,“context”:“cheer”}"}}’);
console.log( obj.type);

What do you mean by “parse that part as JSON again” (assuming you’re talking about message) ?

Thank you

I’m not too familiar with JavaScript, but I think you’re getting the error because the escaped quotes (\") are being turned into quotes before parsing. You’ll need to escape the blackslashes:

function testjson () {
  var obj = $.parseJSON('{"type":"MESSAGE","data":{"topic":"channel-bitsevents.XXXX","message":"{\\"user_name\\":\\"metal_babble\\",\\"channel_name\\":\\"cleartonic\\",\\"user_id\\":\\"XXXX\\",\\"channel_id\\":\\"XXXX\\",\\"time\\":\\"2016-09-18T13:54:13.668Z\\",\\"chat_message\\":\\"cheer1 Test 2.\\",\\"bits_used\\":1,\\"total_bits_used\\":2,\\"context\\":\\"cheer\\"}"}}');

  // Parse message as JSON
  var message = $.parseJSON(;

That code also shows how you parse the message and access it’s properties.

Note that you of course only have to escape the backslashes if you put the PubSub data directly into the code like this (I assume for testing), if you actually receive it over the websocket it wouldn’t be necessary.

1 Like

I believe this has worked for me. Thank you very much.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.