0

I am passing valid JSON on the server side (using node.js) but not able to parse on the client side (using jQuery). jQuery.parseJSON returns null. Any assistance will be much appreciated.

Server side-

var message = [{key:"1", count:1},{key:"2", count:2}];
client.publish('update', JSON.stringify(message));

Client side -

socket.on("update", function(data) {
    var obj = jQuery.parseJSON(data);
    alert (obj); // returns null
    // do something
}
9
  • 1
    Can you check what's the data being received? Commented Sep 8, 2013 at 0:55
  • @FabrícioMatté thanks for editing my question. alert(data) returns Object. Commented Sep 8, 2013 at 4:33
  • Well, JSON.parse takes a string, so there's the problem. If you're on Chrome (or Firefox with Firebug), use console.log(data) instead of alert then press F12 to inspect the object properties. Either your JSON string is inside a property of the data object or data is the parsed object already. Commented Sep 8, 2013 at 4:48
  • Exactly! I tried the technique suggested by @JonathanLonowski . See my response in the thread below. Any ideas? Commented Sep 8, 2013 at 5:51
  • 1
    Problem solved! socket.io emits a key value pair (not an array of key value pair). I changed my implimentation and it works now! Thanks. Commented Sep 8, 2013 at 19:29

2 Answers 2

2

[...] now I get a new error message "Uncaught SyntaxError: Unexpected token o"

That error usually means that data is already the Array parsed from the JSON. You can verify this with typeof:

console.log(typeof data);
// if `string`, then it has yet to be parsed
// if `object`, then it's already been parsed

So, you may not need to use $.parseJSON() or JSON.parse() and may be able to iterate data:

for (var i = 0, l = data.length; i < l; i++) {
    console.log('Key = %s, Count = %s', data[i].key, data[i].count);
}

And, the error comes from JSON.parse() expecting a String:

  1. Let JText be ToString(text)

So, passing an Array of Objects to it will use .toString() to produce:

"[object Object],[object Object]"

It'll start trying to parse this as another Array due to the leading [, but will find object invalid since JSON doesn't support identities.

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

1 Comment

Thanks for the explanation. Much appreciated. Applied changes suggested but still having problems. Below is more information. alert(typeof data); // returns Object alert (data.length); // returns undefined // Hard-coding l=2 for now for (var i = 0, l = 2; i < l; i++) { alert(data[i].key); //Console error: Uncaught TypeError: Cannot read property 'key' of undefined } Intresting. Any ideas?
1

What version of jQuery are you using? From the $.parseJSON documentation:

Prior to jQuery 1.9, $.parseJSON returned null instead of throwing an error if it was passed an empty string, null, or undefined, even though those are not valid JSON.

5 Comments

I was using 1.7.1 . I just changed to 1.9.1 now I get a new error message "Uncaught SyntaxError: Unexpected token o"
I have a feeling that socket is not a valid jQuery object.
socket libraries are included from node.js .
Ah, okay. I don't use node.js
@JonathanLonowski How can I get the key value pairs?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.