0

I have this JSon string received from an AJAX call:

{status:OK,addresses:[0,1,2,3,4,5]}

To convert it to a JSon object I have this line:

var jsonObj = eval(jsonString);

But an exception is thrown! This one has no message in the exception variable. I also tried using

{"status":"OK","addresses":[0,1,2,3,4,5]}

And, yet again, an exception is thrown but saying that an unexpected character '&' was found.

I'm using Struts2 and the JSon is received from an action.

Any help will be appreciated. Thank you

4
  • 2
    That is not a JSON string. It isn't even a valid JavaScript object literal. The second version is JSON and I'd expect eval to handle it (but eval should be avoided, go get json2.js). Since there is no & anywhere in the data, it must be coming from code you haven't provided. Since we can't see that, we can't tell you how to fix it. Commented Jul 20, 2011 at 15:11
  • So I verified that when JSon is received from the request, all the " are replaced by " ... could this be the problem? Commented Jul 20, 2011 at 15:44
  • Yes! The parser is expecting JSON not HTML encoded JSON! Commented Jul 20, 2011 at 15:46
  • So... How can I avoid that it arrives as encoded HTML? I only have a JSP that prints a property defined in the Strut Action. Commented Jul 20, 2011 at 15:55

7 Answers 7

3
{status:OK,addresses:[0,1,2,3,4,5]}

is not valid JSON because the quotes around status and addresses are missing, and is neither valid JSON nor valid JavaScript since the quotes around OK are missing.

Also, don't use eval to parse JSON - it allows an attacker to execute arbitrary JavaScript in the context of your page. Instead, use the safe alternatives JSON.parse(built-in in modern browsers and other EcmaScript 5 implementations) or JSON2.

Sign up to request clarification or add additional context in comments.

1 Comment

@Stargazer712 Could you provide a source for that? The official grammar at json.org is pretty clear about quotes being required. Also, Python's, Chromium's parser, and trivialjson (my parser ;) ) reject {a:1}.
1

Don't use eval: use a proper JSON parser such as JSON2.

You probably have extra content in the response: check that you are not printing anything else out.

Comments

0

This is working for me:

JSON.parse('{ "status" : "OK", "addresses" : [0,1,2,3,4,5]}');

Comments

0

If you want to use eval, then you need to use the second example you posted ({"status":"OK","addresses":[0,1,2,3,4,5]}) and you need to surround the string with parenthesis as such:

var jsonObj = eval( '('+jsonString+')' );

This makes jsonString a valid javascript statement.

With that being said, I encourage you use JSON.parse, as many others have posted. It is far more secure.

2 Comments

The parens don't seem to be required for me. See jsfiddle.net/jfriend00/FcSKR.
@jfriend00, that is because you did NOT define a JSON string, but rather you defined a literal JavaScript object. jsfiddle.net/FcSKR/2
0

You don't have a JSON string. You do have an object literal. You need the names to have quotes.

{"status":OK, "addresses":[0,1,2,3,4,5]}

1 Comment

The OP wasn't using a JSON parser, just eval so really just needed it to be valid javascript which requires quotes around the "OK", not the object keys.
0

Based on this comment:

So I verified that when JSon is received from the request, all the " are replaced by " ... could this be the problem?

Yes. A JSON parser expects to receive JSON as input, not HTML encoded JSON.

Comments

0

Two issues to fix:

  1. Add quotes around the "OK" to make it a legal javascript string.
  2. Add parens around the string before sending to eval like this eval("(" + jsonString + ")")';

This:

{status:OK,addresses:[0,1,2,3,4,5]}

would have to be changed to this:

{status:"OK",addresses:[0,1,2,3,4,5]}

to be valid Javascript (note the quotes around "OK").

It should be this to be valid JSON (quotes around the keys too):

{"status":"OK", "addresses":[0,1,2,3,4,5]}

OK all by itself is not a known piece of Javascript without the quotes around it to make it into a Javascript string. In the future, you can test yourself in a small test bed and see what the error is in your favorite javascript debugger:

http://jsfiddle.net/jfriend00/FcSKR/

var jsonString = '{"status":"OK","addresses":[0,1,2,3,4,5]}';
var jsonObj = eval("(" + jsonString + ")");
alert("success");

If you still get an error with {"status":"OK","addresses":[0,1,2,3,4,5]} and the adding of parens before sending to eval, then your data isn't what you think it is and you need to do some debugging to see exactly what is in the response (look at the value in the debugger, put the value into an alert, etc...). There is probably some other stuff in the response that you didn't know would be there.

Note: there are some situations where a JSON parser like JSON.parse() and a legal JSON string is safer than eval() with Javascript that can be anything.

4 Comments

That is not a JSON string, but a literal JavaScript object. Parenthesis are required. jsfiddle.net/FcSKR/2
OK, I added the parens around it, though my post was mostly just discussing Javascript and the use of eval since that's what the OP is using.
You truly don't understand. You STILL are not doing anything remotely close to what he is asking. Please look at the fiddle that I posted. It explains everything about what you are doing wrong.
My mistake. My fiddle was assigning var jsonString = {"status":"OK","addresses":[0,1,2,3,4,5]}; which is not a string and is why I didn't catch that the parens were required. I've fixed up my post. Thanks for catching my error. So, there two issues to make it work with eval (quotes around "OK" and parens around the whole thing) and a third issue to make it valid JSON (quotes around the object keys).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.