0

I'm getting an 'Uncaught SyntaxError' while trying to parse a JSON string, and I can't figure out why.

It looks like message is a string, which seems to be a common problem, and the json appears to be valid. A snippet of my code is given. It fails on the var obj = ... line.

this.send = function (message) { console.log(message); console.log(message.toString()); console.log('{"ReadyToGo":1}'); console.log(typeof message); var obj = $.parseJSON(message); } 

On the console, I get this just before the error.:

{"ReadyToGo":1} {"ReadyToGo":1} {"ReadyToGo":1} string 

Any ideas?

EDIT: Added console.log(typeof message), which yields 'string'

12
  • 1
    Do console.log(typeof message); Commented Jan 23, 2014 at 5:00
  • 1
    What is the value of message when you pass it? Commented Jan 23, 2014 at 5:01
  • 1
    @Hrishi: He's doing it for the purpose of comparison to the other two console.log()s above it. Commented Jan 23, 2014 at 5:04
  • 2
    Since you're getting "string" from typeof message, then the only thing I could guess would be that there's some invalid invisible character. What happens if you do console.log(message === '{"ReadyToGo":1}');? Commented Jan 23, 2014 at 5:13
  • 2
    stackoverflow.com/questions/18130081/… suggests that your error message is incomplete -- jQuery provides the actual error character at detection. What's the full error? Commented Jan 23, 2014 at 5:29

1 Answer 1

0

OK, I got this sorted. A null character ( '\0' ) was been appended to the string somewhere along the long call chain, and this wasn't visible in the debugger. Thanks to cookie monster for the heads-up on this one.

Everything works as soon as I strip out the null char.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.