JSON.parse choking on valid JSON
09 November 2023 at 9:48 am
I just had a problem where JSON.parse crashed with the classic error “Unexpected token in JSON”, but as opposed to normal it wasn’t on position 0 or 1 (because the server returned HTML rather than JSON). The crash happened right in the middle of a string value at position 42?
I instantly tested the JSON with JsonLint and it was all valid JSON. So how could this happen? The string was sent from my Node.js backend to an ESP32 device and back as an MQTT message. After an hour of not getting anywhere, I asked on X and my friend Jon spotted the culprit after looking closer at the Buffer with the MQTT message. Somehow, the buffer contained a backspace character? How did that happen then?
I traced the error back to the ESP32 device and surely - I’d done a bad copy of a char array. It was referencing stray memory, so virtually anything could have happened to this. It’s happened before and it’ll likely happen again, but not in this way I guess (given that I learned anything). And - I’ve now installed a Gremlins Tracker that might make it easier to see these invalid characters.
So if you have what looks like perfectly valid JSON crashing, have a closer look at what you’re ACTUALLY parsing. In this case, the clue was to look at the buffer object parsed to look for anything that isn’t a valid ASCII text character.