2

I got this JSON format dataset in a file data.json:

{
    "timestamp": "Wed Apr 02 2014, 19:03:19",

    "test": [   
        441.02,
        null,
        null,
        460.99,
        485.91,
        501.0,
        null,
        null,
        null,
        null
    ],

    "test1": [
        437.0,
        null,
        null,
        464.989,
        488.52,
        499.996,
        null,
        null,
        null,
        null
    ]
}

And I need to push that values to an array[] in JavaScript to get something like:

var test = [441.02, null, null, 460.99, 485.91, 501.0, null, null, null, null]

var test1 = [437.0, null, null, 464.989, 488.52, 499.996, null, null, null, null]

Can someone point me in the right direction? I'm a JavaScript newbie.

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
mdv
  • 925
  • 3
  • 14
  • 28
  • 3
    did you mean var test = [441,523,null,null]? – Amaury Medeiros Apr 06 '14 at 18:55
  • [a guide on javascript basics](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide) is probably the right direction, amirite? lolol – guest Apr 06 '14 at 18:57
  • I meant var test = [441.002,null,null,460.99,485.91,501.0,null,null,null] All the values in test, etc etc. – mdv Apr 06 '14 at 18:58
  • Possible duplicate: [How to parse JSON in JavaScript](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript) – Sumner Evans Apr 06 '14 at 19:08

3 Answers3

0

Assuming your JSON is associated with a variable named obj, you can do the following:

for (var key in obj) {
    eval(key + " = " + array[obj]);
}

EDIT:

Alternative:

keys = Object.keys(obj)
for (var i = 0; i < keys.length; i++) {
    eval(keys[i] + " = " + obj[keys[i]]);
}
Amaury Medeiros
  • 2,093
  • 4
  • 26
  • 42
  • 1
    Note: `eval()` could be evil in some situations. (See [this SO question](http://stackoverflow.com/questions/197769/when-is-javascripts-eval-not-evil)). – Sumner Evans Apr 06 '14 at 20:39
0

First, you must load the JSON and then parse it using JSON.parse():

var xmlhttp = new XMLHttpRequest(),
    me = this,
    test = null,
    test1 = null;

// Note: change data.json to any JSON file
xmlhttp.open('GET', 'data.json', true);
xmlhttp.onreadystatechange = function() {
    // If you got something from the request
    if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
        // Parse the JSON from data.json
        var obj = JSON.parse(xmlhttp.responseText);
        me.test = obj.test;
        me.test1 = obj.test1;

        // Do manipulation here. You can call to other functions here.
        doOtherStuff();
    }
};

// Send the request to get the data
xmlhttp.send(null);

function doOtherStuff() {
    console.log(me.test); // This will work because it is called from inside the function
}

References:

Get Json Array with pure JS and Parse JSON in JavaScript?.

NOTE: You must do all data manipulation inside of the if statement inside the onreadystatechange function because the request is asynchronous. The onreadystatechange function is only fired when the ready state is changed. The if statement is only true if you get data from the request. If you try to access the data from outside if statement, it will not work. Here's a little diagram showing why:

   Request Made using xmlhttp.send(null)
|--Code after xmlhttp.send(null) is executed
|  Request complete
|  Request onreadystatechange function is called (readyState = 4 && status = 200)
v
v
v (Code continues to execute)

Notice how the code after xmlhttp.send(null) will probably be executed before the request is complete? That's why you must do the data manipulation inside the onreadystatechange function.

EDIT: To make the array globally accessible, declare it in a global scope, then set it inside the onreadystatechange function. Call any functions that need test from inside the onreadystatechange function. (See above edits to code.)

Community
  • 1
  • 1
Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
0

If you have alot of tests

var tests = [];
var jsonLength = json.length;
var key;
for(key in json){
    if(typeof json[key] === "object"){
        tests.push(json[key]);
    }
}

var test1 = tests[0];
var test2 = tests[1];
Sajjad Ashraf
  • 3,754
  • 1
  • 34
  • 35