400,714 Members | 2,896 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 400,714 IT Pros & Developers. It's quick & easy.

What the heck is wrong with this JSON??

Ryan
P: n/a
{"POINTID":77902,"MAPID":762,"LONG":-122.21654892,"LAT":"37.1834331019","CITY":"Boulder
Creek","STATE":"CA","DIST":5745.4}

I get an "invalid label" error...

I'm kinda new to this. Thanks!

Jul 12 '06 #1
Share this Question
Share on Google+
5 Replies


web.dev
P: n/a

Ryan wrote:
{"POINTID":77902,"MAPID":762,"LONG":-122.21654892,"LAT":"37.1834331019","CITY":"Boulder
Creek","STATE":"CA","DIST":5745.4}

I get an "invalid label" error...

I'm kinda new to this. Thanks!
You're creating an object literal, in which could contain name value
pairs. The "invalid label" that you're seeing is from using incorrect
syntax.

Names have certain rules, for example, names cannot be any of reserved
keywords, cannot start with a number, and can not include special
characters, except an underscore or dollar sign.

Values can be a string, number, object, array, boolean, or null.

For a solution to your problem, this would be a fix: (formatted for
readability)

{POINTID: 77902,
MAPID: 762,
LONG: -122.21654892,
LAT: 37.1834331019,
CITY: "Boulder Creek",
STATE: "CA",
DIST: 5745.4}

Jul 12 '06 #2

RobG
P: n/a

Ryan wrote:
{"POINTID":77902,"MAPID":762,"LONG":-122.21654892,"LAT":"37.1834331019","CITY":"Boulder
Creek","STATE":"CA","DIST":5745.4}

I get an "invalid label" error...
>From a syntax point of view, nothing. You may have a new line
character or something that is breaking it in your actual code (note
that autowrapping in Google Groups has introduced one). Try
re-formatting it:

var s = {
"POINTID":77902,
"MAPID":762,
"LONG":-122.21654892,
"LAT":"37.1834331019",
"CITY":"Boulder Creek",
"STATE":"CA",
"DIST":5745.4
};

var t = [];
for (p in s){
t.push(p + ' : ' + s[p]);
}

alert(t.join('\n'));
--
Rob

Jul 13 '06 #3

Richard Cornford
P: n/a
web.dev wrote:
Ryan wrote:
>>
{"POINTID":77902,"MAPID":762,"LONG":-122.21654892,"LAT":"37.1834331019",
"CITY":"Boulder
>Creek","STATE":"CA","DIST":5745.4}

I get an "invalid label" error...

I'm kinda new to this. Thanks!

You're creating an object literal, in which could contain name
value pairs. The "invalid label" that you're seeing is from
using incorrect syntax.
Although some browser error messages seem obscure they often actually
provide a better clue to what is happening that it may at first appear.
In this case "invalid label" is a better clue than it appears as in
javascript/ECMAScript a label is used to identifier a point in the code
(expected to correspond with the start of a loop construct of some sort)
and consists of an Identifier followed by a colon. If an object literal
was interpreted in a context where what was intended to be a property
name in the form of a string literal was interpreted as a label (because
of the following colon) then it would be invalid and "invalid label"
would be a very direct and informative error message.

The problem here is likely that a string containing this apparent object
literal definition is begin passed directly to the - eval - function and
so is being interpreted as an entire javascript Program. The text of an
object literal definition in the wrong context can be interpreted as a
javascript Program. The surrounding braces become a Block statement, and
the contained name value pairs then look like labelled expression
statements. Under this interpretation most object literals would include
syntax errors and so fail to execute but some can happily (if
pointlessly) be executed as a javascirpt Program, e.g.:-

{
Anything: 555
}

- could happily be interpreted as a javascript Program; A block
statement surrounding a labelled expression statement consisting of a
number literal expression.

Indeed in the form above the 'object literal definition' cannot be
interpreted as an object literal as it commences with an opening brace,
which is explicitly forbidden as the starting token of an Expression
Statement.

For an object literal definition to be interpreted as an object literal
it needs to be unambiguously an expression. This can be done by an
action as simple as surrounding the object literal in parentheses (a
statement cannot be contained by the grouping operators, only an
expression may), or making the object literal the right hand side of an
assignment.
Names have certain rules, for example, names cannot be any
of reserved keywords, cannot start with a number, and can not
include special characters, except an underscore or dollar sign.
This is not true. Identifiers must follow these rules but property names
can consist of any arbitrary sequence of zero or more characters. This
is manifest in the object literal syntax by allowing Identifiers, string
literals and numeric literals to be used as the names of the name/value
pairs (though Mac IE 5 goes belly up if you try to use numeric literals
in that context). The quoted property names above are completely legal,
and probably represent an automated process wrapping the property names
in quotes so that it does not have to think about whether they would
qualify as Identifiers (though making them string literals would still
require the escaping of characters like line terminators).
Values can be a string, number, object, array, boolean, or null.
Or undefined, or functions, regular expressions, dates, etc (if a
distinction is to be drawn between arrays and objects).
For a solution to your problem, this would be a fix: (formatted for
readability)

{POINTID: 77902,
MAPID: 762,
LONG: -122.21654892,
LAT: 37.1834331019,
CITY: "Boulder Creek",
STATE: "CA",
DIST: 5745.4}
If the error had been - expected } or : - then maybe, but this chance
may make the label valid but will then move the error further down the
code to where the second colon is out of place in on expression in a
list expression.

Richard.
Jul 13 '06 #4

Ivan Marsh
P: n/a
On Wed, 12 Jul 2006 14:58:15 -0700, Ryan wrote:
{"POINTID":77902,"MAPID":762,"LONG":-122.21654892,"LAT":"37.1834331019","CITY":"Boulder
Creek","STATE":"CA","DIST":5745.4}
Boulder Creek? Cool dude.
Excellent beer.

--
The USA Patriot Act is the most unpatriotic act in American history.
Feingold-Obama '08 - Because the Constitution isn't history,
It's the law.

Jul 13 '06 #5

Ryan
P: n/a
Thanks for all the feedback. I found the fix, and I think it's related
to Richard's comments.

I *was* passing that explicit string to eval() and getting "invalid
label". What I found was that by enclosing the string with "(" and ")",
it worked.

I'll have to spend a bit of time re-reading what your explanation was,
but this fixed the problem.

Thanks!

Ivan Marsh wrote:
On Wed, 12 Jul 2006 14:58:15 -0700, Ryan wrote:
{"POINTID":77902,"MAPID":762,"LONG":-122.21654892,"LAT":"37.1834331019","CITY":"Boulder
Creek","STATE":"CA","DIST":5745.4}

Boulder Creek? Cool dude.
Excellent beer.

--
The USA Patriot Act is the most unpatriotic act in American history.
Feingold-Obama '08 - Because the Constitution isn't history,
It's the law.
Jul 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.