SyntaxError while eval()'ing an object

I'm trying to parse an object like that, using nodejs :

{
  1: [a, b, c],
  2: [d, e, f],
  ...
}

with a,b,c,d,e,f variables with defined values.

I really want that so that the object can really be readable (and writable by a human).

So, what I'm currently doing is opening a file containing the previous object, putting the data in a variable data, and then injecting

var a=1,b=2,c=3,d=4,e=5,f=6,...;

just before the real content :

data = "var a=1,b=2,c=3,d=4,e=5,f=6,...;" + data;

Finally, I'm using eval() to get a usable object :

my_obj = eval(data);

However, this does not work (at least using nodejs), with a SyntaxError on the : token after the second element of the object (so right after the 2 in the previous example).

To fix that, I'm now surrounding my object with :

data = "var obj = " + data + "; obj";
data = "var a=1,b=2,c=3,d=4,e=5,f=6,...;" + data;

And with that, it works.

The question is why ?

The optional question is : do you have a better way to accomplish what I want ?

Thanks!


Real data before eval() call

data with SyntaxError :

var a='a',b='b',c='c',d='d',e='e',f='f';
{
  a: 
  [a, a, a],

  b:
  [b, b, b]

}

Nodejs error :

undefined:6
  b:
   ^
SyntaxError: Unexpected token :

Working data :

var a='a',b='b',c='c',d='d',e='e',f='f';
var obj = {
  a: 
  [a, a, a],

  b:
  [b, b, b]

}

; obj;

when you first eval

{
 a: 
 [a, a, a],
 b:
 [b, b, b]
}

the brackets are in fact considered as block delimiters delimiting a block (as in if () { ... })

so you are trying to execute the code

 a: [a, a, a], b: [b, b, b];

which clearly is a Syntax error => you get the "SyntaxError: Unexpected token :"

In the second case, the evaluated code is valid javascript, which is why it works.

It is difficult to advise you on a better alternative. Maybe use JSON.parse instead of direct evaluation if your file cannot be trusted.