1

I have the following object

            {   
                    value: 20, 
                    color:"#878BB6"
            },  
            {   
                    value : 40, 
                    color : "#4ACAB4"
            } 

loaded from a text file abc.txt in my local directory in the server.

I want to convert this into an array object. I tried doing

    var string = "{   
                    value: 20, 
                    color:"#878BB6"
            },  
            {   
                    value : 40, 
                    color : "#4ACAB4"
            }"

     var array = JSON.parse("[" + string + "]");
     alert(array);

Nothing happens unfortunately. Help appreciated !

4
  • Maybe it is: var array = JSON.parse("[" + string + "]"); Commented Feb 13, 2015 at 15:17
  • Sorry, I wrote it incorrectly. You are right, I used the round braces, still didn't work. Commented Feb 13, 2015 at 15:23
  • 1
    JSON.parse won't work because it's not parsable JSON. You need to ensure that both key/value pairs are quoted appropriately. Commented Feb 13, 2015 at 15:34
  • Ah ha, just saw Chris' edit. Commented Feb 13, 2015 at 15:35

1 Answer 1

2

You can use "eval" to accomplish what you are attempting.

var s = '{value: 20,  color:"#878BB6" },' +
        '{value : 40,  color : "#4ACAB4"}';

var arr = eval('[' + s + ']');

alert(arr[0].value);

Also, in order for JSON.parse to parse it the string needs to be valid JSON. So you'll need to have quotes around the object property names. Like the following:

var s = '{"value": 20, "color":"#878BB6" },' +
        '{"value": 40, "color": "#4ACAB4"}';

var arr2 = JSON.parse('[' + s + ']');
alert(arr2[1].value);

Although it would be better to modify the process for generating the text file to contain valid JSON if you can. Then you could use jQuery or some other method of just loading the JSON from the file directly.

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

6 Comments

I have used the string variable as it is in my example, thanks a lot. You made my day !
Pulling from the file using ajax you don't need to reformat it. I just formatted it in my snippet to be easier to read and show that it's a string.
JavaScript doesn't support line breaks in a string literal, you'd have to use "/n" to put them in the string.
@user42826, but it does need to parse like Chris said in his answer, which your data doesn't, so you'll need to reformat that.
A related question, I have loaded the text file from the server using : --------------------------------------------- var data2; $.get("testJSON.txt",function(data){ alert("Data from JavaScript file: " +data+ "typeof object " + typeof data); data2 = eval('[' + data+ ']'); }); alert(data2); alert(data2) doesn't seem to work. Suggestions ? I would like to use the data copied from text file passed to an array variable to be used in JavaScript.
|

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.