Solved:

$.getJSON('contacts.json', function (json) {
var array = [];
for (var key in json) {
    if (json.hasOwnProperty(key)) {
        var item = json[key];
        array.push({
            name: item.Name,
            surname: item.Surname,
            mobile: item.mobile,
            email: item.email
        });            
    }
}
});
Answer from Kakitori on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_json_parse.asp
JSON.parse()
AJAX Intro AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples JS JSON · JSON Intro JSON Syntax JSON vs XML JSON Data Types JSON Parse JSON Stringify JSON Objects JSON Arrays JSON Server JSON PHP JSON HTML JSON JSONP JS jQuery
🌐
Katecpp
katecpp.github.io › load-json
How to load external .json to array in js
To ensure that some operations are done after the JSON is fully loaded, use .done callback. var allQuestions = new Array(); function loadQuestions() { $.getJSON('question.json', function (data) { allQuestions = data.quiz; }) .error(function() { console.log('error: JSON not loaded'); }) .done(function() { console.log( "JSON loaded!"
🌐
Stack Overflow
stackoverflow.com › questions › 73400619 › convert-data-from-json-file-to-javascript-array
Convert data from JSON file to JavaScript Array? - Stack Overflow
If you already have the file contents as a JavaScript string (which I assume based on your question), you can just use JSON.parse(). It will parse the JSON into an array of JavaScript objects as you want it.
🌐
EncodedNA
encodedna.com › javascript › convert-data-in-json-file-in-an-array-using-javascript-or-jquery.htm
How to convert data in JSON file to an Array using JavaScript or jQuery
Now, let’s see how you can accomplish ... for this kind of task. That’s what I am using here in my second example. The .getJSON() method uses an Ajax HTTP GET request to read an external JSON file....
🌐
W3Schools
w3schools.com › js › js_json_arrays.asp
JSON Arrays
In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.
🌐
Quora
quora.com › How-do-I-convert-JSON-file-to-Javascript-Array
How to convert JSON file to Javascript Array - Quora
Answer (1 of 8): A JavaScript object or value is converted to a JSON string using the JSON.stringify() method, with the option to replace values if a replacer function or replacer array is given, or to include only the specified attributes otherwise.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › read-json-file-using-javascript
Read JSON File Using JavaScript - GeeksforGeeks
In JavaScript, there are multiple ways to read and parse JSON files. These methods can be used both in browser environments and in Node.js. The fetch() API retrieves JSON files asynchronously and parses them into JavaScript objects.
Published   March 28, 2023
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › parse
JSON.parse() - JavaScript | MDN
The Object, Array, string, number, boolean, or null value corresponding to the given JSON text. ... Thrown if the string to parse is not valid JSON. JSON.parse() parses a JSON string according to the JSON grammar, then evaluates the string as if it's a JavaScript expression.
🌐
Stack Overflow
stackoverflow.com › questions › 46570839 › read-data-from-array-loaded-with-data-within-json-file
javascript - Read data from array loaded with data within JSON file - Stack Overflow
function getJsonData() { var arrJson = []; $.getJSON("data.json", function( data){ arrJson = data; }); return arrJson; }; $(document).ready(function(){ var data = []; data = getJsonData(); //console.log(data); $.each(data, function(i, value){ ...
Top answer
1 of 2
5

You seem to have the order wrong, data is the array so it's not

data.name[i]

but

data[i].name

and removing the strange apply it would be

$.getJSON('json.js',function (data) {   
    for (var i = 0; i < data.length; i++) {
        name.push( data[i].name );
        address.push( data[i].address );
        city.push( data[i].city );
    }
});

Also note that name is a bad choice for a variable name in the global context.

2 of 2
1

You have two or three problems.

Your JSON is invalid.

Remove the quotes from around the outside of it.

Seriously consider giving is a .json file extension, then you are more likely to get the right MIME type for JSON. (JSON data files are not JavaScript programs).


File Json : "data.json"

[
{"name ": "John", "address": "350 Fifth Avenue", "city ": "New York"},
{"name ": "Mark", "address": "1101 Arch St", "city ": "Philadelphia"},
{"name ": "Jack", "address": "60th Street", "city ": "Chicago"}
]

You are accessing your data in the wrong order

This is overly complicated and is accessing properties in the wrong order.

name.push.apply(name, data.name[i]);

Forget about using apply. You don't need it.

Then note that your JSON array contains the objects, not the other way around. You need to access the array index before the property name:

name.push(data[i].name);

Remember the A in Ajax

Finally - your code doesn't show how you are testing the result. Remember that Ajax is asynchronous, so that if you examine the values of your arrays outside the callback function, they might not have been populated yet.

🌐
Webdevtutor
webdevtutor.net › blog › typescript-read-json-file-into-array
How to Read JSON File into Array Using TypeScript
Now, you can call this function with the path to your JSON file as an argument to read its content into an array. const jsonArray = readJSONFileIntoArray('data.json'); console.log(jsonArray);
🌐
ScrapingBee
scrapingbee.com › webscraping-questions › json › how-to-parse-a-json-file-in-javascript
How to parse a JSON file in JavaScript? | ScrapingBee
The above code reads a JSON file and iterates over the key-value pairs in the object. If any of the values are a list or a nested object, it recurses until all the values are simpler data types.
🌐
Quora
quora.com › How-do-you-read-JSON-file-objects-into-an-array
How to read JSON file objects into an array - Quora
Answer: Q:How do you read JSON file objects into an array? Answer More or less the same for Swift or C#: here is the flutter dart example Now, read it into “IT”, the array: … ABC Those Classes, were generated by QuickType IO. Let me repeat, 1 more time. Me, don’t know J-things, don’t kn...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn › JavaScript › Objects › JSON
Working with JSON - Learn web development | MDN
If you load this JSON in your JavaScript program as a string, you can parse it into a normal object and then access the data inside it using the same dot/bracket notation we looked at in the JavaScript object basics article. For example: ... First, we have the variable name — superHeroes. Inside that, we want to access the members property, so we use .members. members contains an array populated by objects.
🌐
Stack Overflow
stackoverflow.com › questions › 43638167 › how-to-navigate-json-file-get-desired-object-and-push-it-into-array-using-javas
How to navigate JSON file, get desired object and push it into array using javascript? - Stack Overflow
If you want to push the whole thing onto an array, it doesn't matter how big it is (at least until you start running out of memory) ... i read the file from firebase, the json i provide is the actual output from JSON.stringify() command except ...
🌐
freeCodeCamp
freecodecamp.org › news › json-stringify-example-how-to-parse-a-json-object-with-javascript
JSON Stringify Example – How to Parse a JSON Object with JS
January 5, 2021 - The easiest way to get data from an API is with fetch, which includes the .json() method to parse JSON responses into a usable JavaScript object literal or array automagically.