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

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jul 12, 2006 at 8:15 AM // reply »
1 Comments

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


Jul 12, 2006 at 8:34 AM // reply »
74 Comments

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


ryan
Jul 12, 2006 at 6:25 PM // reply »
1 Comments

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


Jul 12, 2006 at 6:37 PM // reply »
74 Comments

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.


Aug 8, 2006 at 8:15 AM // reply »
1 Comments

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.


Aug 8, 2006 at 8:20 AM // reply »
74 Comments

Elzr, glad I could help :)


happy reader
Aug 18, 2006 at 4:30 AM // reply »
1 Comments

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


Truong Vo
Aug 22, 2006 at 8:18 PM // reply »
1 Comments

Thanks you, saved me with this info.


Aug 22, 2006 at 9:30 PM // reply »
74 Comments

TV,

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


Titus
Sep 7, 2006 at 5:50 AM // reply »
1 Comments

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


Sep 9, 2006 at 12:39 AM // reply »
1 Comments

Wow, thank you for saving me a miserable night.


Luca
Nov 28, 2006 at 9:11 AM // reply »
6 Comments

Thank you very much for the hint !


Dec 3, 2006 at 5:40 AM // reply »
1 Comments

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


locoHost
Dec 3, 2006 at 12:19 PM // reply »
1 Comments

You are my hero!!!!

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

:-)


Dec 4, 2006 at 1:26 PM // reply »
1 Comments

Thank you thank you thank you!


Dec 13, 2006 at 8:28 AM // reply »
1 Comments

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. :)


DH
Dec 16, 2006 at 9:37 AM // reply »
1 Comments

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

!!Thank You!!


Stefan
Dec 30, 2006 at 7:59 AM // reply »
19 Comments

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.


Jan 11, 2007 at 9:33 AM // reply »
1 Comments

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.


Jan 11, 2007 at 4:17 PM // reply »
5,406 Comments

Till,

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


Jan 18, 2007 at 5:21 PM // reply »
1 Comments

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?


Jan 19, 2007 at 11:29 PM // reply »
5,406 Comments

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.


JL
Jan 23, 2007 at 8:53 PM // reply »
1 Comments

Thanks,

It helped me.


Jan 24, 2007 at 9:06 AM // reply »
1 Comments

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.


Jan 24, 2007 at 3:01 PM // reply »
5,406 Comments

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.


Andrew Norris
Feb 13, 2007 at 6:58 PM // reply »
1 Comments

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!


Julien
Feb 15, 2007 at 2:02 AM // reply »
1 Comments

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

Thanks !


Mar 4, 2007 at 6:05 PM // reply »
1 Comments

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


j.r.
Mar 6, 2007 at 3:37 PM // reply »
1 Comments

thanks; this was really frustrating. :-)


phoneynk
Mar 14, 2007 at 5:50 AM // reply »
1 Comments

Thanx it helped me too :)


bud
Apr 1, 2007 at 5:37 PM // reply »
2 Comments

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


Apr 2, 2007 at 3:50 PM // reply »
5,406 Comments

@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.


Apr 2, 2007 at 11:03 PM // reply »
2 Comments

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


Apr 3, 2007 at 7:04 AM // reply »
5,406 Comments

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


May 1, 2007 at 5:38 PM // reply »
1 Comments

You helped me too. good man. good man!


Ray Anderson
May 5, 2007 at 2:16 PM // reply »
1 Comments

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

Your solution worked for me also!!!!


Phil Pelanne
Jun 6, 2007 at 2:22 PM // reply »
1 Comments

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


T Gregory
Jun 15, 2007 at 9:25 AM // reply »
1 Comments

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


Lance Fisher
Jun 26, 2007 at 1:15 AM // reply »
1 Comments

Thanks for writing this!


Brian Lacy
Jul 5, 2007 at 8:37 PM // reply »
1 Comments

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". ;)


Jul 6, 2007 at 7:18 AM // reply »
5,406 Comments

@Brian,

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


Jon
Jul 12, 2007 at 8:42 AM // reply »
1 Comments

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


Peter Robinett
Oct 7, 2007 at 10:25 AM // reply »
1 Comments

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.


Sushil
Oct 9, 2007 at 3:35 PM // reply »
1 Comments

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


Oct 31, 2007 at 10:46 PM // reply »
1 Comments

save my day :-)

thanks for the tip


S. Woodward
Nov 12, 2007 at 9:18 AM // reply »
1 Comments

Wheee, thanks - saved me some head bashing :)


Nov 14, 2007 at 11:08 AM // reply »
1 Comments

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.


Nov 14, 2007 at 11:59 AM // reply »
5,406 Comments

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


Anton
Nov 21, 2007 at 4:51 AM // reply »
1 Comments

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


Nov 22, 2007 at 5:21 PM // reply »
1 Comments

i guess you saved me from a sleepless night!!

thanks a lot!

sincerely yours,
Sven Buschbeck


Nov 27, 2007 at 5:24 PM // reply »
1 Comments

Thanks man!

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


Dec 26, 2007 at 4:31 PM // reply »
1 Comments

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!


Dec 31, 2007 at 3:37 PM // reply »
1 Comments

thanks for saving me a lot of wasted time :)


Tony
Feb 15, 2008 at 12:12 PM // reply »
2 Comments

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


Matt Tyson
Feb 20, 2008 at 11:57 AM // reply »
1 Comments

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


Josh Nathanson
Mar 5, 2008 at 3:45 PM // reply »
17 Comments

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!


mrtoe
Mar 25, 2008 at 9:37 AM // reply »
1 Comments

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')


Richard
Mar 25, 2008 at 4:26 PM // reply »
1 Comments

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


Seth L
Apr 24, 2008 at 6:41 PM // reply »
1 Comments

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


Holger_P
May 20, 2008 at 12:00 PM // reply »
1 Comments

Hey Man, i spent 4 hours on this - now its all working THX TO YOU!
lol you rock!


LS
Jun 23, 2008 at 3:08 AM // reply »
1 Comments

But why does this solve the problem. Why do we require parenthesis.


Jul 1, 2008 at 11:17 AM // reply »
1 Comments

This tip works perfectly for me, too.

Thanks for my saved hours.


Récupération De Données
Jul 1, 2008 at 8:20 PM // reply »
1 Comments

Wow, thanks a lot! This solved my problem!

This error started to drive me crazy...


Robin
Jul 8, 2008 at 5:28 AM // reply »
1 Comments

Dude, your solution worked for me. I was pulling my hair out! Thanks a lot for posting this.


Jul 8, 2008 at 8:09 AM // reply »
5,406 Comments

@Robin,

Glad to have helped.


butter
Jul 12, 2008 at 4:43 AM // reply »
1 Comments

though i think every thinnk is resolvedfor this - but let me know if i can eb of some help. i have done few of tutorials on this , may be a little help i can follow


Jon
Aug 18, 2008 at 10:08 PM // reply »
1 Comments

Dude...after a day of beating my head on the wall, this was it. Thanks!!


wschwarz
Oct 13, 2008 at 12:33 PM // reply »
1 Comments

thanks, your tip solved my problem


Oct 31, 2008 at 5:50 PM // reply »
1 Comments

Add my name to the thousands of grateful developers who's been beating his head on the wall! What gets me, is that it has been working perfectly fine for *days* without the parens. Now, suddenly, it requires the parens. Perhaps a tiny, subtle difference in the data made the difference between working and not working.


Nov 2, 2008 at 3:08 PM // reply »
5,406 Comments

@Gary,

As always, Im glad to have helped.


Joe
Nov 6, 2008 at 9:37 AM // reply »
1 Comments

Over 2 years on and still helping people with this.
Thanks for posting it.


Nov 6, 2008 at 10:18 AM // reply »
5,406 Comments

@Joe,

It's the gift that keeps on giving ;)


Sabri Onur Tuzun
Jan 6, 2009 at 7:12 AM // reply »
1 Comments

Thanks a lot!


pingu0121
Jan 8, 2009 at 1:31 AM // reply »
1 Comments

thx man i had the same problem.......


Jan 9, 2009 at 11:03 AM // reply »
1 Comments

Thanks a lot! I wouldn't have figured this out in a million years.


Matt Huggins
Apr 4, 2009 at 7:36 PM // reply »
1 Comments

Wow, thanks for that! I feel like a moron now. ;)


Sriram
Apr 8, 2009 at 7:01 AM // reply »
1 Comments

Hi ,
I am getting this JSON object from my code ( a servlet)
[{"name_one": "select_one","value_one": -1}]

I am syntax error when I send this as arg in my callback function..

$.getJSON(
getThis().baseURL()+"citiesForWidget.htm?format=json&_jsoncallback=?",
function(data){
alert(data);
});

Any idea ??


Apr 9, 2009 at 8:54 AM // reply »
5,406 Comments

@Sriram,

I am not seeing anything suspicious in your code. Can you check the actual return value via FireBug to see what value is coming through in the SCRIPT tag.


Ian
May 15, 2009 at 4:28 AM // reply »
1 Comments

This really saved my ass last night.

You're still the Google number 1 on this issue.
Thanks loads.


Post Comment  |  Ask Ben

Recent Blog Comments
Justice
Jul 3, 2009 at 11:10 PM
Create A Running Average Without Storing Individual Values
@Ben, I think you're going about this the wrong way. You're trying to use complicated techniques when there is a simple and beautiful technique readily available (a la Gary Funk's comment). Instead ... read »
Bob
Jul 3, 2009 at 9:19 PM
Project HUGE: Huge In A Hurry - Get Big - Phase 3 / Week 1
a good technical explanation http://crossfitphoenix.typepad.com/crossfit_phoenix_forging_/the-overhead-squat.html ... read »
Jul 3, 2009 at 9:03 PM
Create A Running Average Without Storing Individual Values
If I wanted to do this and only carry two numbers, I'd keep track of the sum and N. Then you are pretty much accurate all the time. average = (sum + new_number) / (N + 1) But all this was in a for ... read »
Roland Collins
Jul 3, 2009 at 8:58 PM
Create A Running Average Without Storing Individual Values
@Martin - not just floating point though. Depending on what langauge you're working in, decimals can cause just as many headaches if they're not precise enough. But again, for most applications, th ... read »
Isnogood
Jul 3, 2009 at 7:16 PM
Project HUGE: Huge In A Hurry - Get Big - Phase 3 / Week 1
Watch this http://www.nsca-lift.org/videos/default.shtml ... read »
Aaron
Jul 3, 2009 at 7:13 PM
Project HUGE: Get Big, Phase One (Chat Waterbury - Huge In A Hurry)
I've just finished the 3rd week of phase 3, and have to agree that the overhead squats are hard. I think this is most due to the wide grip on which places more pressure on your upper back. Only this ... read »
Isnogood
Jul 3, 2009 at 7:11 PM
Project HUGE: Huge In A Hurry - Get Big - Phase 3 / Week 1
Very good, there were some near perfect reps, and there were some dodgy ones, but you're getting there your body position is good. Work on your depth and do not let the bar move forward or backward, ... read »
Martin Mädler
Jul 3, 2009 at 6:48 PM
Create A Running Average Without Storing Individual Values
Nice dodge! I dig the idea to force out the last bit of performance out of a chunk of code, even though it's such a minor thing. Heard of this kinda approach in connection with "running sums". @Rola ... read »