1

I'm having trouble decoding an array of strings from what I thought was JSON.

var result = [
  "{gene: 'PEX2', go_bp: '0.766500871709', CombinedPvalue: '9.999999995E-4'}",
  "{gene: 'PEX5', go_bp: '0.766472586087', CombinedPvalue: '9.999999995E-4'}",
  "{gene: 'PEX7', go_bp: '0.766386859737', CombinedPvalue: '9.999999995E-4'}"
];

You can see that there are 3 gene-related strings of JavaScript object literals, each encoded as a string. How can I decode these?

I tried JSON.parse but it gives me an error.

for (var i = 0; i < result.length; i++) 
    console.log(JSON.parse(result[i]));

Uncaught SyntaxError: Unexpected token g.

Is there a simple way?

4
  • 6
    What you have is not JSON. Seems like strings containing JS object literals. The best solution would be to fix the code that generates these strings. Commented Jun 16, 2015 at 2:10
  • It is an array of almost-JSON. See json.org for proper format. Commented Jun 16, 2015 at 2:11
  • JSON string should be in this format: {"key": "value"} not {key : "value"}. Commented Jun 16, 2015 at 2:12
  • 1
    It's also likely that you don't want to quote the numeric values. Commented Jun 16, 2015 at 2:24

3 Answers 3

5

Since this is valid javascript, you can use Function() to return a new instance of the object by creating an anonymous function and then immediately executing it. Unlike the other answer with eval(), you don't have to declare a variable and assign the object literal to that variable in the string passed to eval - everything you need can be done cleanly in one line.

var result = [
    "{gene: 'PEX2', go_bp: '0.766500871709', CombinedPvalue: '9.999999995E-4'}",
    "{gene: 'PEX5', go_bp: '0.766472586087', CombinedPvalue: '9.999999995E-4'}",
    "{gene: 'PEX7', go_bp: '0.766386859737', CombinedPvalue: '9.999999995E-4'}"
];

for (var i = 0; i < result.length; i++) {
    // create function that returns the obj literal
    // and call it immedieately.
    var obj = new Function( 'return ' + result[i] + ';' )();
    document.write('gene: ' + obj.gene + ' <br />');
}

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

2 Comments

Excellent use of Function to avoid an eval.
It's not really avoiding an eval, it's just using another way to do it.
3

What you have is not JSON, but Javascript objects in text form. You can convert them to Javascript objects with eval():

    var result = [
      "{gene: 'PEX2', go_bp: '0.766500871709', CombinedPvalue: '9.999999995E-4'}",
      "{gene: 'PEX5', go_bp: '0.766472586087', CombinedPvalue: '9.999999995E-4'}",
      "{gene: 'PEX7', go_bp: '0.766386859737', CombinedPvalue: '9.999999995E-4'}"
    ];

    var f;
    for (var i = 0; i < result.length; i++) {
        eval("f = "+result[i]);
        console.log(f.gene);
    }

Note: eval is generally held to be evil. In this case it's safe enough if you're absolutely sure that the source array will only ever hold data, and no code.

Comments

1

The JSON format requires double quotes around property names. Your sample data lacks these quotes, and this is not valid JSON.

It also requires double quoted values, not single quoted.

Try something like this:

   '{"gene": "PEX2", "go_bp": "0.766500871709", "CombinedPvalue": "9.999999995E-4"}',

Comments

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.