Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What I am trying to do is simple. Parse this array holding json objects into a Javascript array.

var merchantsJson = JSON.parse('[{"id":61693,"name":"Más"},{"id":61690,"name":"\u0027\u0022\u003C/div\u003E"}]');

But the unicode character \u003C seems to be breaking the parser. In the chrome console I see "Uncaught SyntaxError: Unexpected token <"

A little more info. The above is what the code is evaluated to. In reality the code contains a jsp expression.

var merchantsJson = JSON.parse('${jsonArr}');

If I remove the single quotes, there is no issue, but eclipse give me an "missing semicolon" error message. Is it possible to parse the array with the quotes as I am trying to do?

share|improve this question
    
You're getting a lot of responses similar to Juhana's comment, but note that JSON is not actually a subset of JavaScript. There are edge-cases where valid JSON will not be parsed correctly by a javascript interpreter that come about when you're working with Unicode: stackoverflow.com/questions/23752156/… - be careful with this advice –  Aaron Dufour yesterday
    
@AaronDufour I'm pretty sure JSP's JSON stringifier can handle those cases, considering it's designed to have its output inserted "as is" to the JS code. –  Juhana yesterday
    
@Juhana I'm not familiar with JSP's JSON stringifier, so I figured a general warning was justified. If it can be trusted to \u-encode troublesome characters, then there won't be a problem. –  Aaron Dufour yesterday
    
@AaronDufour Seeing from the question that it encodes characters like < and >, it's a rather safe bet that it also encodes characters that are known to break compatibility. –  Juhana yesterday
    
@Juhana The variable name jsonArr indicates that OP has already done the stringifying, and I was not willing to assume that said stringification was aware of the intricacies of JSON vs JavaScript regarding Unicode. I see now that that is merely a misleading name. –  Aaron Dufour yesterday

4 Answers 4

The interpolation of ${jsonArr} is already a JavaScript object. When you wrap it in '${jsonArr}' this turns it into a string and you have to use JSON.parse.

There's no need to make it a string. You can just do var merchantsArray = ${jsonArr}. JSON constructs are already interoperable with JavaScript code.

share|improve this answer
3  
Eclipse will complain that it's not legal JavaScript, but that's to be expected because it isn't JavaScript. It's a template that will produce JavaScript. –  ikegami yesterday
2  
This is the correct answer. Everyone else is treating the symptoms, not the cause. –  Juhana yesterday
4  
@Juhana, This is a correct answer if the JSON is trusted and if it's actually legal JS (which not all JSON is). If it's not trusted, the correct answer is to properly convert the JSON into a JS string literal, which is what the other answers are suggesting (or trying to). They are not, as you say, treating the symptoms. –  ikegami yesterday
2  
@ikegami Yes, and you're misinterpreting it. It says that while those characters are illegal as symbols, they're legal when encoded (i.e. "foo\u2028bar" is legal both as JSON and as JS string.) –  Juhana yesterday
2  
The JSON is not trusted, that is why it is quoted. –  mad_fox yesterday

Try to replace \u with \\u. If you don't, JSON parser receives already decoded Unicode, which created polluted JSON.

share|improve this answer
4  
There's noting wrong with decoded Unicode in JSON... in fact it requires Unicode. The only real problem is that one character represents a double quote, which closes the "sub-string" that the parser is interpreting. That's the only one that needs escaping. –  squint yesterday

Because there's an extra " in your string literal that is encoded by \u0022:

> '[{"id":61693,"name":"Más"},{"id":61690,"name":"\u0027\u0022\u003C/div\u003E"}]'
[{"id":61693,"name":"Más"},{"id":61690,"name":"'"</div>"}]

In short, your JSON in the string is invalid. You would need to escape the unicode escape sequences for the quotes in the string literal ("'\u0022</div>"), by using

JSON.parse('[{"id":61693,"name":"Más"},{"id":61690,"name":"\u0027\\u0022\u003C/div\u003E"}]'
//                                                               ^

or escape the quote character ("'\"</div>"):

JSON.parse('[{"id":61693,"name":"Más"},{"id":61690,"name":"\u0027\\\u0022\u003C/div\u003E"}]');
//                                                               ^^

However, there actually is no need to use JSON at all. Just output a JS array literal into your code:

var merchantsJson = ${jsonArr};
share|improve this answer
    
Just wanted to note that the extra \ in your last parsing example isn't really needed since JSON too will interpret the escape sequence. Works either way, just thought I'd mention it. –  squint yesterday
    
@squint: Yeah, that's like the first solution then :-) I maybe should've used \\\" for extra clarity. –  Bergi yesterday
    
Yes, except that it unnecessarily escapes the single quotes, which have no special meaning to JSON, so those could just be received by the parser as actual quote characters instead of escape sequences. Again, just noting it for people because this escaping stuff can be confusing. It's all good and it all works. :-) –  squint yesterday

It's not because of \u003C, rather the \u0022 character is causing the issue, since it's a quotation mark and JavaScript treats it literally ending the string.

You need to escape that character: \\u0022 .

share|improve this answer
    
\u0027 isn't the problem; \u0022 is. –  Aaron Dufour yesterday
1  
Yes, both can be the problem depending on how you start your string. –  AlejandroC yesterday
3  
@AlejandroC: No, only the double quote is the problem. Doesn't matter what quotes you're using in the string literal because it's already an escape sequence, so it won't break the main string. –  squint yesterday
1  
Thanks @squint stackoverflow.com/questions/19176024/… –  AlejandroC yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.