Shaun Mccran

My digital playground

23
M
A
R
2010

What does "Invalid Label" mean in a JSON response, and how can I fix it?

I recently build an application that returned a JSON object of data based on a select field value. Whilst I was building this I encountered an error using the JavaScript eval() function on my JSON response. I am using the eval() function to parse a string from my JSON response, but firebug is showing a JavaScript error of "Invalid Label".

This is the code:

view plain print about
1function(data){
2    jsonResponse = eval(data);
3    var src = jsonResponse.DATA;
4    });

After having a Google around it appears that the eval function is interpreting the first item in the JSON response as a label, rather than data. By changing the code like this:

view plain print about
1jsonResponse = eval("(" + data + ")");

We are forcing eval to recognise the data as variable data. Now the JSON to string translation works.

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Ben G.'s Gravatar A really good tip. I will make use of it. Did you observe any performance improvement after using it?

I'll note it down, am sure it will be useful for us.
# Posted By Ben G. | 07/07/2010 12:02
Back to top