-4

Trying to access the value field of this JSON file using JSON.parse() in Meteor, but I cannot get it to return anything. I suspect there is an error in my syntax in selecting the data from the imported JS object.

{"status":"success","data":{"subjects":[{"value":"ABC","descr":"Descriptions"}]},"message":null,"meta":{"copyright":"Copyright","referenceDttm":"Date"}}

I'm trying to store it into an array, subjectArray. This is the code I'm using:

var subjectArray = new Array();
subjectFile = HTTP.get("https://classes.cornell.edu/api/2.0/config/subjects.json?roster=FA15");
subjectJSON = JSON.parse(subjectFile);
for (int i=0; i<subjectJSON.length; i++) {
  subjectArray.push(subjectJSON[i].value)
}

Pretty printed this is:

{
    "data": {
        "subjects": [
            {
                "descr": "Descriptions",
                "value": "ABC"
            }
        ]
    },
    "message": null,
    "meta": {
        "copyright": "Copyright",
        "referenceDttm": "Date"
    },
    "status": "success"
}
Charlie X.
  • 17
  • 4

1 Answers1

0

Responses from HTTP calls can take a while to come back so you should read your replies in an async way. You should move all of your code related to the "get" inside a callback function. If you want to find out more about HTTP and callbacks in Meteor make sure you check the docs

If you know what "value" is in your for loop ('cause I don't), then this is your answer:

HTTP.get("https://classes.cornell.edu/api/2.0/config/subjects.json?roster=FA15", function (err, res) {
  if(!!err) return false; 
  subjectJSON = JSON.parse(res);
  for (var i = 0; i < subjectJSON.length; i++) {
    subjectArray.push(subjectJSON[i].value);
  }
  return true;
});

Also, there is no int in JavaScript.

Radu Chiriac
  • 1,374
  • 3
  • 18
  • 34