Your condition in the for loop does not allow you to add anything to your array. Instead, check to see if your json object has a property, then get the value and add it to your array. In other words:
if (questions.hasOwnProperty(key)) should be if (json.hasOwnProperty(key))
In addition, you cannot simply return to get the result of such an AJAX call, because the method runs asynchronously. This return actually applies to the internal callback to the success function, not getArray . You must use the callback template to transfer data only after receiving it and act accordingly.
(Of course, since the array is defined in the outer scope, you would not need to return it anyway, but if you try to use it before the AJAX method ends, it will be empty.)
Assuming you are going to display it in the DOM using a method called renderJSON :
var questions = []; function getArray(){ $.getJSON('questions.json', function (json) { for (var key in json) { if (json.hasOwnProperty(key)) { var item = json[key]; questions.push({ Category: item.Category }); } } renderJSON(questions); }); }
source share