problem with eval and JSON

G

graphicsxp

Hi,

I'm trying to use eval to handle JSON data returned by my webservice.
The data looks like :

{"results": [ {"id": 4}]}

and my eval is as followed :

eval('('+v+')') // v contains the string above

It seems like valid JSON data to me, but I keep getting this error :

Microsoft JScript compilation error: Expected ']'


Any idea ?

Thanks
 
M

Martin Honnen

graphicsxp said:
I'm trying to use eval to handle JSON data returned by my webservice.
The data looks like :

{"results": [ {"id": 4}]}

and my eval is as followed :

eval('('+v+')') // v contains the string above

It seems like valid JSON data to me, but I keep getting this error :

Microsoft JScript compilation error: Expected ']'

The following works without problems for me

var v = '{"results": [ {"id": 4}]}';

var o = eval('('+v+')');

alert(o.results[0].id);

Tested with IE 7.
 
G

graphicsxp

graphicsxp said:
I'm trying to use eval to handle JSON data returned by my webservice.
The data looks like :
{"results": [ {"id": 4}]}
and my eval is as followed :
eval('('+v+')')    // v contains the string above
It seems like valid JSON data to me, but I keep getting this error :
Microsoft JScript compilation error: Expected ']'

The following works without problems for me

var v = '{"results": [ {"id": 4}]}';

var o = eval('('+v+')');

alert(o.results[0].id);

Tested with IE 7.

It's really strange, if I use a hardcoded value like you did, it
works. However my string is coming back from a webservice, and
although it looks exactly the same as the hardcoded one, I get this
annoying error ....
 
R

RoLo

graphicsxp said:
I'm trying to use eval to handle JSON data returned by my webservice.
The data looks like :
{"results": [ {"id": 4}]}
and my eval is as followed :
eval('('+v+')')    // v contains the string above
It seems like valid JSON data to me, but I keep getting this error :
Microsoft JScript compilation error: Expected ']'
The following works without problems for me
var v = '{"results": [ {"id": 4}]}';
var o = eval('('+v+')');
alert(o.results[0].id);

Tested with IE 7.

        Martin Honnen
       http://JavaScript.FAQTs.com/

It's really strange, if I use a hardcoded value like you did, it
works. However my string is coming back from a webservice, and
although it looks exactly the same as the hardcoded one, I get this
annoying error ....

try,
alert(v+"\nLength: "+v.length);
and see if the string length is the "correct" length... maybe a
whitespace character is causing problems. Do a "trim" on the string
before eval'uating it.

I would suggest using firefox + firebug for debugging before testing
on IE.
firebug has a net feature where you can see the data received using
ajax.
 
P

Peter Michaux

graphicsxp wrote:
I'm trying to use eval to handle JSON data returned by my webservice.
The data looks like :
{"results": [ {"id": 4}]}
and my eval is as followed :
eval('('+v+')')    // v contains the string above
It seems like valid JSON data to me, but I keep getting this error :
Microsoft JScript compilation error: Expected ']'
The following works without problems for me
var v = '{"results": [ {"id": 4}]}';
var o = eval('('+v+')');
alert(o.results[0].id);
Tested with IE 7.
It's really strange, if I use a hardcoded value like you did, it
works. However my string is coming back from a webservice, and
although it looks exactly the same as the hardcoded one, I get this
annoying error ....

try,
alert(v+"\nLength: "+v.length);
and see if the string length is the "correct" length... maybe a
whitespace character is causing problems.

Along the same lines of examining the string: try a loop over the
length of the string to inspect the code point values. Something like
the following.

var codepoints = []
for (var i=0, ilen=str.length; i<ilen; i++) {
codepoints.push(str.charCodeAt(i));
}
alert(codepoints.join(', '));

You can then compare to the hard coded string and see exactly what is
the difference.

Peter
 
T

Thomas 'PointedEars' Lahn

RoLo said:
graphicsxp said:
Martin said:
graphicsxp wrote:
I'm trying to use eval to handle JSON data returned by my webservice.
The data looks like :
{"results": [ {"id": 4}]}
and my eval is as followed :
eval('('+v+')') // v contains the string above
It seems like valid JSON data to me, but I keep getting this error :
Microsoft JScript compilation error: Expected ']'
The following works without problems for me
var v = '{"results": [ {"id": 4}]}';
var o = eval('('+v+')');
alert(o.results[0].id);
Tested with IE 7.
[signature snipped]

Please trim your quotes.
try,
alert(v+"\nLength: "+v.length);
and see if the string length is the "correct" length...

There is no "correct" length, ECMAScript Programs are of arbitrary length.
maybe a whitespace character is causing problems.

Whitespace is optional and irrelevant if not in a literal, e.g.

{ "results" : [ { "id" : 4 } ] }

and

{"results":[{"id":4}]}

are equivalent in an ECMAScript Program.
Do a "trim" on the string before eval'uating it.

Unnecessarily inefficient. Do you even know what eval() does?
I would suggest using firefox + firebug for debugging before testing
on IE.

That might not be possible, so Microsoft Script Debugger and Firebug Lite
are options that are available for free.
firebug has a net feature where you can see the data received using
ajax.

XHRs ("AJAX") can be also be logged in Firebug's Console tab using the
Options menu.


PointedEars
 
R

RoLo

!!!Casting anti-PointedEars protection spell!!!
There is no "correct" length, ECMAScript Programs are of arbitrary length..
Could you please explain more on this?
maybe a whitespace character is causing problems.

Whitespace is optional and irrelevant if not in a literal, e.g.

  { "results" : [ { "id" : 4 } ] }

and

  {"results":[{"id":4}]}

are equivalent in an ECMAScript Program.
but... since its IE7... my "maybe" stands valid!
Unnecessarily inefficient.  Do you even know what eval() does?
Didn't get this one... evaluates a string perhaps?
The webservice could be sending garbage before or after the "json".
Not that using trim would be the ideal solution (or even work), but
this is a JS newsgroup, not a
..net or php one :p
That might not be possible, so Microsoft Script Debugger and Firebug Lite
are options that are available for free.
touche
 
T

Thomas 'PointedEars' Lahn

RoLo said:
!!!Casting anti-PointedEars protection spell!!!

You should hear yourself -- "*anti*-PointedEars *protection*"?

You better cast a wannabe protection spell, and subsequently disappear in a
puff of logic.
Could you please explain more on this? instead

eval() evaluates the argument, converted to a String if necessary, as if it
was an ECMAScript Program. Read the Spec.
maybe a whitespace character is causing problems.
Whitespace is optional and irrelevant if not in a literal, e.g.

{ "results" : [ { "id" : 4 } ] }

and

{"results":[{"id":4}]}

are equivalent in an ECMAScript Program.
but... since its IE7... my "maybe" stands valid!

Do you have any conclusive evidence that whitespace matters in JScript
5.7.5xxx Object or Array initializers, and especially in a string to be
passed to eval(), IOW that these particular versions of this particular
implementation are not standards-compliant in that regard?
Didn't get this one... evaluates a string perhaps?

Not just any string; it must be in the form of an ECMAScript(-compliant)
Program or a SyntaxError exception is thrown. And if eval() is called in an
inappropriate way, implementations may throw an EvalError exception. See
the ECMAScript Language Specification, Edition 3 Final, section 15.1.2.1.
The webservice could be sending garbage before or after the "json".

That's a distinct possibility, however the word `trim' with regard to
strings is usually associated with trimming leading and trailing
*whitespace* (see the FAQ), and the OP explicitly said that the value the
server responded with was exactly the same as posted. That rather indicates
(unprintable) whitespace, if any, and trimming it would/should not (see
above) have any effect.
Not that using trim would be the ideal solution (or even work),

If you are not convinced that it would be a good idea or even work, why do
you recommend it then?
but this is a JS newsgroup, not a ..net or php one :p

What does it have to do with that? Are you mistaken in the regard that
because there are evidently even more JS script-kiddies than .NET or PHP
ones, it would be appropriate to give (intentionally) misleading or wrong
answers here?


Score adjusted

PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
eval() evaluates the argument, converted to a String if necessary, as if it
was an ECMAScript Program. Read the Spec.

BTDT. With eval(x), if `x' is not a string, `x' is returned instead. No
implicit string conversion takes place, of course. But that matters not for
the problem discussed.


PointedEars
 
R

RoLo

You should hear yourself -- "*anti*-PointedEars *protection*"?

Maybe if you had heard me when I said it you would had notice my lack
of seriousness.
You better cast a wannabe protection spell, and subsequently disappear in a
puff of logic.

Now, that would have been interesting hearing after I said that!

----------------
YOUR reference:

15.1.2.1 eval (x)
When the eval function is called with one argument x, the following
steps are taken:
1. If x is not a string value, return x.
2. Parse x as a Program. If the parse fails, throw a SyntaxError
exception (but see also clause 16).
3. Evaluate the program from step 2.
4. If Result(3).type is normal and its completion value is a value V,
then return the value V.
5. If Result(3).type is normal and its completion value is empty, then
return the value undefined.
6. Result(3).type must be throw. Throw Result(3).value as an
exception.
If value of the eval property is used in any way other than a direct
call (that is, other than by the
explicit use of its name as an Identifier which is the
MemberExpression in a CallExpression), or if the
eval property is assigned to, an EvalError exception may be thrown.

PointedEars:
There is no "correct" length, ECMAScript Programs are of arbitrary length.

In what I wrote, v is supposed to be a string!
eval() evaluates the argument, converted to a String if necessary, as if it
was an ECMAScript Program. Read the Spec.

Doesn't convert it! it IS a string, check #1. If its not returned, its
a string.
Do you have any conclusive evidence that whitespace matters in JScript
5.7.5xxx Object or Array initializers, and especially in a string to be
passed to eval(), IOW that these particular versions of this particular
implementation are not standards-compliant in that regard?

No I don't. I should have said invalid character... but, How would you
explain then, that the string the guy gets is the "same"
as the hardcoded one but gets the error? could be a not closed [ but
he says its the same
string! I haven't seen you share solutions here.
Not just any string; it must be in the form of an ECMAScript(-compliant)
Program or a SyntaxError exception is thrown. And if eval() is called in an
inappropriate way, implementations may throw an EvalError exception. See
the ECMAScript Language Specification, Edition 3 Final, section 15.1.2.1.

Interesting.. and how would you get this error:
Microsoft JScript compilation error: Expected ']'

That's a distinct possibility, however the word `trim' with regard to
strings is usually associated with trimming leading and trailing
*whitespace* (see the FAQ), and the OP explicitly said that the value the
server responded with was exactly the same as posted. That rather indicates
(unprintable) whitespace, if any, and trimming it would/should not (see
above) have any effect.


If you are not convinced that it would be a good idea or even work, why do
you recommend it then?

Since there are not a trim method defined on any popular browser that
I know...
I was thinking in something on the line:
var s=("\n\n {'results': [ {'id': 4}]} \n\n"+String.fromCharCode
(1000)),e;
//this:
e=eval("("+s.substring(s.indexOf('{'),s.lastIndexOf('}')+1)+")");
// Instead of this
e=eval("("+s+")"); // Check what error firefox gets here,
interesting...

// Still no code from you.
What does it have to do with that? Are you mistaken in the regard that
because there are evidently even more JS script-kiddies than .NET or PHP
ones, it would be appropriate to give (intentionally) misleading or wrong
answers here?

No, not at all and Peter's reply confirms I'm not the only one
shooting at invalid (invisible) characters.
My point was that because he is asking how to solve this on JS help
him in JS. Would also be better if he finds the error in JS first.

On the other hand, "there are evidently even more JS script-kiddies
than .NET or PHP ones".
Score adjusted

how old are you?

Rolo
 
L

Logos

By the way, what is the score now?


I think the important part is he thinks he is winning.

Peter

Ah yes - PointedEars does so enjoy launching the nukes at even the
vaguest possibility of a threat :D

For what it's worth, I think examining the returned string is the way
to start. There may be a hidden illegal character hiding in there
somewhere. Perhaps convert the string to hex and display the results
alongside the hex string for the hardcoded version to look for diffs?
 
T

Thomas 'PointedEars' Lahn

Apparently you are unable to quote properly. That does not bode well for
your record in this newsgroup.
Maybe if you had heard me when I said it you would had notice my lack
of seriousness.
Whatever.


Now, that would have been interesting hearing after I said that!

It should be your reference, too, as it would prevent you from jumping to
conclusions, like:
15.1.2.1 eval (x)
When the eval function is called with one argument x, the following
steps are taken:
1. If x is not a string value, return x.
2. Parse x as a Program. If the parse fails, throw a SyntaxError
exception (but see also clause 16).
[...]

PointedEars:
There is no "correct" length, ECMAScript Programs are of arbitrary length.

In what I wrote, v is supposed to be a string!

Strings are also of arbitrary length (only limited by available heap memory)
in ECMAScript implementations. Can't you read?
Doesn't convert it!

True, I have corrected myself 4 minutes after, 4 hours 41 minutes before
your reply. (Has anyone told you already that you shouldn't use Google
Groups for posting to Usenet if you can't handle it?)
it IS a string, check #1. If its not returned, its a string.

As I said in my correction, that doesn't matter here, though. The string,
when passed (as here), is parsed *as if* it was an ECMAScript Program, and
both are specified (and implemented) to be of arbitrary length (else we
would not have to deal with about 28 KiB of Prototype.js junk, for example.)
Do you have any conclusive evidence that whitespace matters in JScript
5.7.5xxx Object or Array initializers, and especially in a string to be
passed to eval(), IOW that these particular versions of this particularfour
implementation are not standards-compliant in that regard?

No I don't. I should have said invalid character... but, How would you
explain then, that the string the guy gets is the "same"
as the hardcoded one but gets the error? could be a not closed [ but
he says its the same string!

He could be mistaken. Trimming anything client-side isn't supposed to solve
the problem in the long run, though. The server output has to be fixed, if
erroneous, instead.
I haven't seen you share solutions here.

There are no solutions to share when the input (or here: the output) is that
uncertain.
Not just any string; it must be in the form of an ECMAScript(-compliant)
Program or a SyntaxError exception is thrown. And if eval() is called in an
inappropriate way, implementations may throw an EvalError exception. See
the ECMAScript Language Specification, Edition 3 Final, section 15.1.2.1.

Interesting.. and how would you get this error:
Microsoft JScript compilation error: Expected ']'

Another distinct possibility is that the OP did not post the exact value.
Since there are not a trim method defined on any popular browser that
I know...

See above. The FAQ has one (and it is rather easy to devise one by
yourself), so that would not matter. It is the method of debugging that is
in question here, though, not how it is accomplished.
I was thinking in something on the line:
var s=("\n\n {'results': [ {'id': 4}]} \n\n"+String.fromCharCode
(1000)),e;

Why you parenthesize the assigned value of `s' here, remains a mystery.
//this:
e=eval("("+s.substring(s.indexOf('{'),s.lastIndexOf('}')+1)+")");

As I said, garbage before or after is a possibility. But given this string
`s', trimming it is unnecessary as whitespace (such as newlines) is _not_
garbage, and it is irrelevant outside of literals in ECMAScript Programs,
until you provide proof to the contrary for one implementation. So far,
you have only vented a lot of hot air here, like:
// Instead of this
e=eval("("+s+")"); // Check what error firefox gets here,
interesting...

FTR, the error message I get in Firebug is:
| SyntaxError: missing ) in parenthetical

That's interesting, indeed, because

(

{'results': [ {'id': 4}]}

Ϩ)

("Ϩ" is the return value of String.fromCharCode(1000) or U+03E8, COPTIC
CAPITAL LETTER HORI, but that matters little as long it isn't whitespace)
just isn't a syntactically valid ECMAScript Program. Why should it?
// Still no code from you.

See above.
No, not at all and Peter's reply confirms I'm not the only one
shooting at invalid (invisible) characters.

Indeed, you should read my postings more thoroughly. Does "distinct
possibility" ring a bell?
My point was that because he is asking how to solve this on JS help
him in JS.

Your argument is flawed. If any such output is possible, the server output
must be checked and fixed there instead.
Would also be better if he finds the error in JS first.

Second step before the first one, likely to constitute a waste of time.
[...]
Score adjusted

how old are you?

At least mentally, apparently much older than you.


PointedEars
 
R

RoLo

Strings are also of arbitrary length (only limited by available heap memory)
in ECMAScript implementations.  Can't you read?

And IE uses ECMAScript?

The length checking was in the alert, not eval.
See above.  The FAQ has one (and it is rather easy to devise one by
yourself), so that would not matter.  It is the method of debugging that is
in question here, though, not how it is accomplished.

You believe that checking the length of the string, before eval, will
not reflect garbage if it existed?
Your argument is flawed.  If any such output is possible, the server output
must be checked and fixed there instead. ....
Second step before the first one, likely to constitute a waste of time.

You don't use the browser for checking the output then.


Rolo
 
T

Thomas 'PointedEars' Lahn

For the last time, learn to quote. For example, at least the first
quotation level requires an attribution line so that it is obvious whose
text have been quoted.
vvvvvvvvvvv said:
And IE uses ECMAScript?

MSHTML-based browsers like IE support JScript, Microsoft's implementation of
ECMAScript. RTFFAQ and said:
The length checking was in the alert, not eval.

That really doesn't matter.
You believe that checking the length of the string, before eval, will
not reflect garbage if it existed?

Not necessarily. If the client-side implementation is borken, the garbage
could elude it with the `length' property as well.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You don't use the browser for checking the output then.

Hear, hear!

| try,
| alert(v+"\nLength: "+v.length);
^^^^^ ^^^^^^^^
| and see if the string length is the "correct" length... maybe a
^^^^^^^^^^^^^^^^ ^^^^^^^
| whitespace character is causing problems. Do a "trim" on the string
^^^^^^^^^^^^^^^^^^^^ ^^^^^^
| before eval'uating it.

Sounds familiar? Read it in context in
<

PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>, Sun,
1 Feb 2009 13:36:57 said:
For the last time, learn to quote. For example, at least the first
quotation level requires an attribution line so that it is obvious whose
text have been quoted.
vvvvvvvvvvv
RoLo wrote:

Your advice in the subject is inappropriate, Your minimal attributions
fail to comply with accepted Usenet practice. Stick to what you
understand, and, with the time this saved, take lessons in behaving more
like a normal civilised human being, rather than like one of the Stasi.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top