-1

I am trying to use JSON.parse to parse a string to the array of array, such as

var a = "[['1909-23', 'egg']]"  
JSON.parse(a)

It gives me the SytaxError. Wonder if there is any easy way to solve it. Thanks.

dapangmao
  • 2,727
  • 3
  • 22
  • 18
  • 5
    That's not valid JSON. JSON insists on double-quote characters for strings. – Pointy Feb 28 '18 at 15:51
  • Also note that you can just write `var a = [['1909-23', 'egg']];` without involving JSON at all. – Pointy Feb 28 '18 at 15:52
  • @Pointy - chances are that the value is being passed from an external source. – Lix Feb 28 '18 at 15:52
  • 2
    This is almost certainly an [XY problem](http://xyproblem.info). Where is the data coming and why isn't it valid JSON? You should be fixing the actual problem, not the symptoms. – JJJ Feb 28 '18 at 15:53
  • 2
    @Lix yes that's possible, but then again there are countless questions out there involving people making that mistake about what JSON is for. – Pointy Feb 28 '18 at 15:54
  • Sorry. This is just a string with single-quotation from the backend. Not a JSON. – dapangmao Feb 28 '18 at 15:54
  • @dapangmao There are only two possibilities: either the string is generated by some third-party site / service, in which case, you could read their API documentation to figure out how to parse this, or the string is generated by you, in which case, you should just fix it to generate valid JSON. – Sebastian Simon Feb 28 '18 at 15:58
  • 2
    Why does the backend pass data in a non-standard format, and if you know it's not JSON then why would you expect that `JSON.parse()` could handle it? You'll have to write your own parser or fix the backend, and fixing the backend would be a lot simpler. – JJJ Feb 28 '18 at 15:59

2 Answers2

4

The string

"[['1909-23', 'egg']]"

Is not a valid JSON string. As such you can't call JSON.parse() on it.

The JSON format requires double quotes around strings.

A solution would be then to use double quotes:

var a = '[["1909-23", "egg"]]';
console.log(JSON.parse(a));
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
1

Before you use this, please read Why is using the JavaScript eval function a bad idea?. This will potentially open up your JavaScript to code injection attacks. A much better solution is to actually turn your string into correct JSON and parse is using JSON.parse

That all said, you can “parse” (actually you've executing the string as javascript, hence the injection problem) this string using eval.

var a = "[['1909-23', 'egg']]" 
var b = eval(a);
console.log(b);

Note the warning on MDN

Do not ever use eval!

eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, a third-party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible.

eval() is also slower than the alternatives, since it has to invoke the JS interpreter, while many other constructs are optimized by modern JS engines.

Additionally, modern javascript interpreters convert javascript to machine code. This means that any concept of variable naming gets obliterated. Thus, any use of eval will force the browser to do long expensive variable name lookups to figure out where the variable exists in the machine code and set it's value. Additonally, new things can be introduced to that variable through eval() such as changing the type of that variable, forcing the browser to reevaluate all of the generated machine code to compensate. However, there (thankfully) exists a very good alternative to eval: simply using window.Function. As an example of how you convert code using evil eval() to using Function(),

Liam
  • 27,717
  • 28
  • 128
  • 190