Corrupted JSON in message data

FYI Brian mentioned this is a known issue but posting here for awareness.

One thing I noticed is that sometimes the messages seem corrupted and can’t be parsed (invalid JSON).


The JSON looks normal but when I paste into my editor, it looks like this:


I am not sure what these characters are but might be something to check into.

To follow up on this, I’m seeing occasional issues with my data parsing related to null values for DOM prices. Appears most often around market close. Hope a fix is still in the works. Here is an example:


Would definitely prefer to have these null items in the “bids” and “offers” arrays omitted on the server side so they’re sent, like below:


@Brady, I’m not 100% certain these issues are related, but for now ensure that in your code you check for null before performing an operation on your DOM data. There are lot’s of ways to do so in JS. You could use a simple if statement, or even set a default value with the || operator.

const domData = JSON.parse(rawData.slice(1))
domData.forEach(data => {
  const { bids, offers } = data.d
  bids.forEach(bid => {
    //check if price exists (doesn't coerce to falsey, which catches null/undefined)
    if(bid.price) { /*...*/ }
    // or set a default
    const price = bid.price || 0

As for the known corrupt DOM data - what I personally do to prevent this from being an issue in my code is wrap the part of the program that decodes the JSON in a try catch block something like this:

// declare websocket
const WS = new WebSocket(MD_URL)

// add an event listener for the 'message' event
WS.addEventListener('message', function(msg) {
  let data
  // try to parse the JSON here 
  try {
    data = JSON.parse(
  } // logging as necessary in the catch block...
  catch(err) {
    data = []
    console.log('failed to process message: ' + err)

// then we can carry on with the program

This solution will simply omit the corrupt data. Hope someone can find this useful!

1 Like

@Alexander Yep, try…catch has been working well for me with JSON parsing. I hadn’t had any issues until I started logging to CSV files. I changed JSON parsing modules, and the new module converts values it can’t interpret to null. So the eventual exception was pushed down the chain.

In Python, a useful check before storing to dataframes is isinstance(var, int) or isinstance(var, float).
In Julia, I’ve been using the check isa(var, Number).

I think we can mark this issue as resolved, haven’t seen this in quite a while. Curious if anyone else on this thread has seen it?