JSON: Minor But VERY Important Detail Creating Syntax Error: Invalid Label

Posted June 26, 2006 at 6:30 PM

Tags: AJAX, ColdFusion, Javascript / DHTML

I was beating my head against the wall today for a bit trying to figure out why some values coming back from my ColdFusion JSON-esque solution where not evaluation properly. I had the following code in my AJAX data handler:

 Launch code in new window » Download code as text file »

  • this.OnLoad(
  • eval( this.Connection.responseText );
  • );

Damn thing was driving me crazy. Kept all kinds of errors like invalid label name and what not (I don't remember then very well as the head-bashing pretty much cleared those brain cells). Then, suddenly I saw the mistake: I forgot my parentheses:

 Launch code in new window » Download code as text file »

  • this.OnLoad(
  • eval( "(" + this.Connection.responseText + ")" );
  • );

To be honest, I am not sure what exactly the difference is. The parens must help Javascript figure out how and what it is evaluating. Maybe I will figure it out in my next book: Javascript Bible.

Download Code Snippet ZIP File

Comments (59)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Keep your Web site content fresh and your overhead costs low with Savvy Content Manager

Reader Comments

Thanks for the tip, I faced exactly the same problem.
Dunno what the parenthesis do change, either.

Posted by YoGi on Jul 12, 2006 at 8:15 AM


Anything I can do to help :) I am just recently getting into this stuff, so hopefully I will understand what that means eventually.

Posted by Ben Nadel on Jul 12, 2006 at 8:34 AM


dude, THANK YOU! I think you're the only guy on the web that wrote about this.
Love the "de-spam" feature here :)

Posted by ryan on Jul 12, 2006 at 6:25 PM


Thanks Ryan! Glad to help. Thanks also for the de-spamming props. I got so tired of reading those words behind wavey lines that I needed something simple. So far it seems to work perfectly. I get a good number of bots hitting the comments page, but nothing slips through.

Posted by Ben Nadel on Jul 12, 2006 at 6:37 PM


Thanks a lot!

This seems like an extremely common error and yet I had to page through several result pages from Google to get to a website that dealt with it directly.

I guess the error lies in that eval tries to interpret, say, {"kinky":"you got that right"} as a label (which are a pretty obscure JS feature - http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Loop_Statements:label_Statement).
In fact, if you try running <script>{"kinky":"you got that right"}</script> it will also die. So when you add the parentheses you seem to make the statement unambiguous an object to JS.

And looking at Crockford's JSON in Javascript page (http://www.json.org/js.html) I noticed he does include the parentheses in his example:

var myObject = eval('(' + myJSONtext + ')');

But I still think he should point it out in huge bold letters (^_^), it's nonobvious and critical--I'm sending him an email. :)

Anyway, thanks again, you saved me much head-scratching.

Posted by elzr on Aug 8, 2006 at 8:15 AM


Elzr, glad I could help :)

Posted by Ben Nadel on Aug 8, 2006 at 8:20 AM


awesome, you rock! (that was driving me nuts)

Posted by happy reader on Aug 18, 2006 at 4:30 AM


Thanks you, saved me with this info.

Posted by Truong Vo on Aug 22, 2006 at 8:18 PM


TV,

Wicked sweeet. Always glad to help. Please contact me if you have any other issues that are stumping you.

Posted by Ben Nadel on Aug 22, 2006 at 9:30 PM


You have no idea how badly /I/ was hitting my head against the wall. I'm glad it's over now. Thanks!

Posted by Titus on Sep 7, 2006 at 5:50 AM


Wow, thank you for saving me a miserable night.

Posted by Robby Walker on Sep 9, 2006 at 12:39 AM


Thank you very much for the hint !

Posted by Luca on Nov 28, 2006 at 9:11 AM


This page is now top Google hit for "json invalid label". Exactly what I needed - so let me add my thanks to the thousands of others

Posted by Nick Atty on Dec 3, 2006 at 5:40 AM


You are my hero!!!!

Thank you so much for posting this JSON "paren" issue.

:-)

Posted by locoHost on Dec 3, 2006 at 12:19 PM


Thank you thank you thank you!

Posted by David Grant on Dec 4, 2006 at 1:26 PM


Thanks Ben,
Although I was blessed and didn't have to bang by head as there was google and Your post is #1 on "SyntaxError: invalid label".
My body thanks You for skipped self-inflicted injuries. :)

Posted by Konrads on Dec 13, 2006 at 8:28 AM


critical.
non obvious.
you just saved me about a day of brainache!

!!Thank You!!

Posted by DH on Dec 16, 2006 at 9:37 AM


Just want to add my "you saved some hours of my life" thank you :)
would be interesting to know why the parenthesis are necessary though. But at least now I know that they are.

Posted by Stefan on Dec 30, 2006 at 7:59 AM


I managed to create this error in another way:

foo = new Object();
foo.bar = 'foo':

(Note the colon, instead of a sem-colon.)

Drove me almost nutcase.

Posted by Till on Jan 11, 2007 at 9:33 AM


Till,

Thanks for the heads up on other ways to debug the erorr!

Posted by Ben Nadel on Jan 11, 2007 at 4:17 PM


i still get that weird error with dojo from today's svn snapshot:

here's the code in question:

function onclick_myButton()
{
var bindArgs = {
url: "{{ verify_url }}",
mimetype: 'text/javascript',
error: function(type, errObj, evt){
// handle error here
alert('Try again Dude!');
},
load: function(type, data, evt){
// handle successful response here
var jsonObj = eval( '(' + data + ')' );
// myElement = document.getElementById('helloworld');
// myElement.childNodes[0].nodeValue = jsonObj;
// myElement.innerHTML = data;
alert(jsonObj);
},
// might get deprecated in a future release
formNode: document.getElementById("myForm")
};
dojo.io.bind(bindArgs);

... which outputs the following in firebug:

DEBUG: [SyntaxError: invalid label, file: http://localhost:8000/media/js/dojo/dojo.js, line: 114]
...

if i use dojo.json.evalJSON(data) i get similar results, as the
type == error gets defined..

any ideas?

Posted by erob on Jan 18, 2007 at 5:21 PM


Erob,

Sorry, I am not familiar with Dojo. I think that is one of the tradeoffs with using a Javascript framework - its hard to debug where the heck stuff is going wrong. But, looking at the code you posted, I cannot see anything wrong.

Posted by Ben Nadel on Jan 19, 2007 at 11:29 PM


Thanks,

It helped me.

Posted by JL on Jan 23, 2007 at 8:53 PM


If the parenthases are vital to the json being eval'd "correctly" then why aren't they included from the server side?

Is it simply for the obscure case where you actualy do send back a label?

In my work, I have included the paranthases in the xslt that creates the json from xml.

Posted by Stuart Grimshaw on Jan 24, 2007 at 9:06 AM


Stuart,

You ask the tough questions :) I cannot think of any reason why I would not use the parens... so to that effect, I cannot think of a situation where it would be bad to include them on the server side.

The only thing I can think of is a separation of concerns. Meaning, JSON doesn't care what you do with it. The Parens are only for use during evaluation of the JSON data. JSON is just a style of notation and syntax - we just happen to be using it for AJAX and dynamic evaluation.

But other than that, can't think of an argument against what you are saying.

Posted by Ben Nadel on Jan 24, 2007 at 3:01 PM


Thank you -- I was only starting to get frustrated with this when I did a google search on 'javascript "invalid label"', and you were the first result. The fix worked perfectly. Many thanks!

Posted by Andrew Norris on Feb 13, 2007 at 6:58 PM


Hope you will stay for a long time number one on "json invalid label" in Google; -)

Thanks !

Posted by Julien on Feb 15, 2007 at 2:02 AM


lol. i think we found a little bug in your comment parser. =) see links above. thanks again.

Posted by james revillini on Mar 4, 2007 at 6:05 PM


thanks; this was really frustrating. :-)

Posted by j.r. on Mar 6, 2007 at 3:37 PM


Thanx it helped me too :)

Posted by phoneynk on Mar 14, 2007 at 5:50 AM


Here is a sampel of the JSON text being returned from an httpRequest

{"Results":
{"pubNeighborhhods":
{"Rows":[
{"intNeighborHoodID":1,"intBookID":1,"Name":"Blairgowrie","Latitude":"0","Longitude":"0","Country":"Australia","state":"Vic"}
,{"intNeighborHoodID":2,"intBookID":1,"Name":"Crib point","Latitude":"0","Longitude":"0","Country":"Australia","state":"Vic"}
,{"intNeighborHoodID":3,"intBookID":1,"Name":"Moorooduc","Latitude":"0","Longitude":"0","Country":"Australia","state":"Vic"}
]
}
}
}

the above is returned as one long string with no line breaks, i added those for readability

when i use eval() it causes "invalid label" error

I have read about this online and the only answer i find is to enclose the JSON string in parentheses "(" and ")"

when i do this i get the following error
"missing ) in parenthetical"

i have not found a solution to this issue although i have found a couple posts asking about it

thanks

bud

Posted by bud on Apr 1, 2007 at 5:37 PM


@Bud,

It works fine for me:

<cfsavecontent variable="strJSON">
{"Results":{"p............state":"Vic"}]}}}
</cfsavecontent>

<html>
<head>
<title>JSON Test</title>

<script type="text/javascript">

var data = eval(
"(" +
"#JSStringFormat( Trim( strJSON ) )#" +
")"
);

alert( data.Results );

</script>
</head>
<body>
</body>
</html>

I took out all the line breaks and white space from your JSON data. Maybe that is what was causing the problem.

Posted by Ben Nadel on Apr 2, 2007 at 3:50 PM


thanks for looking at it. i may never now what I had messed up but when i took it off my local development serve, i got it to work. i shut down my development computer and restarted.

i was getting the error while running it through a debugger.

the short answer is that it works for me too now with no change in the sorce code??? got to love the .net development envornment...

thanks for the response.

bud

Posted by Bud Hartel on Apr 2, 2007 at 11:03 PM


Oh .NET.. You!!! Good to hear that it is working now.

Posted by Ben Nadel on Apr 3, 2007 at 7:04 AM


You helped me too. good man. good man!

Posted by Dougal Matthews on May 1, 2007 at 5:38 PM


Thanks! Just got done beating my head into the wall thinking my JSON syntax was hosed.

Your solution worked for me also!!!!

Posted by Ray Anderson on May 5, 2007 at 2:16 PM


Another satisfied customer. Not seeing "invalid label" was a beautiful thing.

Posted by Phil Pelanne on Jun 6, 2007 at 2:22 PM


wow - there is a solution for everything on the nets. :) thanks, man... this came in handy!

Posted by T Gregory on Jun 15, 2007 at 9:25 AM


Thanks for writing this!

Posted by Lance Fisher on Jun 26, 2007 at 1:15 AM


I spent a good deal of time tracking down this issue. Thanks for your tremendously useful post!

You'll be happy to know (if you don't already, which you probably do) that the your page is now the #1 spot on Google for the key words "json invalid label" and even "invalid label". ;)

Posted by Brian Lacy on Jul 5, 2007 at 8:37 PM


@Brian,

That's good to hear :) I am just glad that this is helping people!

Posted by Ben Nadel on Jul 6, 2007 at 7:18 AM


Thanks! That is a brilliant insight and one that the Rhino book misses... ;)

Posted by Jon on Jul 12, 2007 at 8:42 AM


Let me also just say thanks. For those interested in why this is happening, I found this explanation useful: http://www.thescripts.com/forum/post1989841-4.html.

Posted by Peter Robinett on Oct 7, 2007 at 10:25 AM


Thanks dude:) that worked. This is crazy, I had a bit struggle.

Posted by Sushil on Oct 9, 2007 at 3:35 PM


save my day :-)

thanks for the tip

Posted by helmi03 on Oct 31, 2007 at 10:46 PM


Wheee, thanks - saved me some head bashing :)

Posted by S. Woodward on Nov 12, 2007 at 9:18 AM


Thanks dude you saved me some headaches :)
There was a lot of errors with IE on pages where I didn't include those parenthesis... Adding those fixed them.

Posted by Jonathan Schemoul on Nov 14, 2007 at 11:08 AM


That's what I like to hear - another head being saved :)

Posted by Ben Nadel on Nov 14, 2007 at 11:59 AM


+1 from me, the same problem, solved in 5- mins thanks to this page.

Posted by Anton on Nov 21, 2007 at 4:51 AM


i guess you saved me from a sleepless night!!

thanks a lot!

sincerely yours,
Sven Buschbeck

Posted by Sven Buschbeck on Nov 22, 2007 at 5:21 PM


Thanks man!

Google-debugging is the best, but it wouldn't work without you.

Posted by Kevin on Nov 27, 2007 at 5:24 PM


I was just about to start my own personal head bashing session when I decided to do a simple search. This page came up first and saved me a couple of hours of headache.

Thanks!

Posted by David Alsbury on Dec 26, 2007 at 4:31 PM


thanks for saving me a lot of wasted time :)

Posted by Aaron Brethorst on Dec 31, 2007 at 3:37 PM


Thanks! I was definitely having a "WTF is PC Load Letter?!" moment with the "invalid label" error.

Posted by Tony on Feb 15, 2008 at 12:12 PM


In addition to thanking the author, I'd like to thank all the posts here for the refreshing blast of positivity ;)

Posted by Matt Tyson on Feb 20, 2008 at 11:57 AM


Almost two years later and this post is still paying dividends. Luckily I didn't bang my head for too long before googling "invalid label". Thanks Ben!

Posted by Josh Nathanson on Mar 5, 2008 at 3:45 PM


if it executes javascript code the eval command wouldnt throw a hissy fit, the problem results when responseText is an object without assignment, anonymous objects require encapsulation with curly braces on instantiation.

if however you don't want to use curly braces this would work too, responseText Object gets assigned to someVar

eval('someVar = '+responseText')

Posted by mrtoe on Mar 25, 2008 at 9:37 AM


Had to try the antispamthingy, dont see any yet though!

Posted by Richard on Mar 25, 2008 at 4:26 PM


Thank you! Hilarious how long this post has lasted and remains useful...

Posted by Seth L on Apr 24, 2008 at 6:41 PM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting