$.getJSON is asynchronous so you should do:

$.getJSON("test.json", function(json) {
    console.log(json); // this will show the info it in firebug console
});
Answer from seppo0010 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › read-json-file-using-javascript
Read JSON File Using JavaScript - GeeksforGeeks
The fetch() API retrieves JSON files asynchronously and parses them into JavaScript objects. ... <html> <head></head> <body> <script> function fetchJSONData() { fetch('./sample.json') .then(response => { if (!response.ok) { throw new Error(`HTTP ...
Published   March 28, 2023
🌐
GitHub
gist.github.com › laurenancona › bd560948d71054e3d1477e43c4d48cb6
Access local JSON data with Javascript · GitHub
For reading the external Local JSON file (data.json) using java script · data = '[{"name" : "Ashwin", "age" : "20"},{"name" : "Abhinandan", "age" : "20"}]'; // Mention the path of the json file in the script source along with the javascript file.
🌐
Oxylabs
oxylabs.io › blog › javascript-read-json-file
How to Read JSON Files in JavaScript: Tutorial
The two syntaxes to load this JSON file are as follows: ... The require() function looks for products.json in the same directory as the script and reads the JSON file in JavaScript object format.
🌐
Reddit
reddit.com › r/learnjavascript › is it really so hard to load a local json file?
r/learnjavascript on Reddit: Is it really so hard to load a local JSON file?
February 18, 2022 -

I'm working on a platformer game in HTML, and have the level data stored in a JSON file. It's right there, in the same folder, and yet I can't find a simple way to access it.
I've seen 2 approaches online, one is to put the entire thing in quotes, then add "data=" to the start, and run it like a JS file. This is beyond stupid, but I'll settle for it if I have to.
The other involves making an actual "server" request to my local machine, but when I try that I get an error in chrome, saying that CORS doesn't allow you to access files using request type null. Other people in the comments were saying it was a bad idea to do that.
Is there seriously no easy way to just say "load this file", without jQuery or node?

Top answer
1 of 3
2
You are using a webserver to access your HTML? fetch('myJson.json').then(res => res.json()).then(json=>console.log(json)); If you're not running a server locally to run your HTML/JS (going to http://localhost), instead just opening the HTML file in the browser (where the URL is like file:///), I'd definitely recommend using a server..
2 of 3
2
Is there seriously no easy way to just say "load this file", without jQuery or node? The other answers describe the how, but to answer the why of this part directly -- it's all about the runtime that the JavaScript code is executing in. jQuery has nothing to do with it (this is just a JavaScript library with some useful functions), but it matters whether the JavaScript is executing inside of a user's web browser, or inside of a server-side environment (including desktop applications). Right now, the "user" is just you developing the game, but the "user" could be anyone. Web browsers do not allow JavaScript code to load arbitrary files from the user's machine. It's not a limitation of the language, it's the runtime (the browser) which disallows this. Imagine if you went to a website and it had JavaScript code that was allowed to read files from your computer! Thankfully, web browsers prevent this from happening. This is why the other comments have suggested hosting the data files (JSON) on a server -- using a NodeJS runtime for example. This runtime allows reading files, because it's not the end-user whose files are being read, it's the developer running the server.
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › how-to-read-json-file-in-javascript
How to Read a JSON File in JavaScript – Reading JSON in JS
November 7, 2024 - One standard method we can use to read a JSON file (either a local file or one uploaded to a server) is with the Fetch API. It uses the same syntax for both.
🌐
Quora
quora.com › In-JavaScript-how-do-I-read-a-local-JSON-file
In JavaScript, how do I read a local JSON file? - Quora
Answer (1 of 13): It depends on what environment we're talking about. JavaScript now runs client- and server-side. Client side JavaScript is mostly found in browsers (Chrome, Firefox, Safari), and IE. Server side JavaScript is primarily nodejs. If you are talking about reading JSON from a local ...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Read local JSON file using only Javascript - JavaScript - The freeCodeCamp Forum
May 6, 2022 - So far I have only found one method to do this, which is creating a GET request for the local file like this: var request = new XMLHttpRequest(); request.open("GET", "localFolder/example.json", false); request.send(null); var data = ...
🌐
DEV Community
dev.to › roshan_100kar › how-to-read-a-local-json-file-using-fetch-api-21jh
How To Read a local JSON File Using Fetch API - DEV Community
September 25, 2022 - On the other hand, if you are doing NodeJS, you have the fs module to actually read the file system. And in both cases, you can statically import JSON. ... Independent front-end developer Designer, Blogger, Web developer #webdev #webdevelopment #webdesign #programminghumor ... Practical functional programmer, types lover, climber, early days violonist. I build the Measur platform: https://www.measur.co/ ... It's very convenient when you need to mock some external service in a test environment. It means you can read a local file without having to conditionally use require or fs and keep a consistent server application
🌐
Squash
squash.io › how-to-read-an-external-local-json-file-in-javascript
How to Read an External Local JSON File in Javascript
To read an external local JSON file in JavaScript, you can use the XMLHttpRequest object or the fetch() function.
🌐
TutorialsPoint
tutorialspoint.com › how-to-import-local-json-file-data-to-my-javascript-variable
How to import local json file data to my JavaScript variable?
We have an employee.json file in a directory, within the same directory we have a js file, in which we want to import the content of the json file.The content of employees.json −employees.json"Employees" : [ { "userId":"ravjy", "jobTitleName
🌐
CodePen
codepen.io › KryptoniteDove › post › load-json-file-locally-using-pure-javascript
Load JSON file locally using pure Javascript by Rich on CodePen
The clue here is the jQuery method $.getJSON() which is shorthand for $.ajax(). It may seem an odd approach requesting a local file in this way but it offers the most flexibility with minimum fuss. function loadJSON(callback) { var xobj = new XMLHttpRequest(); xobj.overrideMimeType("application/json"); xobj.open('GET', 'my_data.json', true); // Replace 'my_data' with the path to your file xobj.onreadystatechange = function () { if (xobj.readyState == 4 && xobj.status == "200") { // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode callback(xobj.responseText); } }; xobj.send(null); } The function above will create a new instance of a XMLHttpRequest and load asynchronously the contents of my_data.json.
🌐
Rayobyte
rayobyte.com › blog › javascript-load-json-file-from-descktop
How to JavaScript Load JSON File From Desktop || Rayobyte
September 23, 2024 - The first method you can use to read a JSON file is the fetch method, also represented as (). This method is used to read local or uploaded JSON files. You will use the same syntax for both types of files.
🌐
CodeHim
codehim.com › home › vanilla javascript › javascript read local json file without jquery
JavaScript Read Local JSON File without jQuery — CodeHim
January 22, 2024 - The following code should be placed within a <script> tag in your HTML document or in an external JavaScript file. (function(){ function onChange(event) { var reader = new FileReader(); reader.onload = onReaderLoad; reader.readAsText(event.target.files[0]); } function onReaderLoad(event){ console.log(event.target.result); var obj = JSON.parse(event.target.result); document.getElementById('fileContent').innerHTML = event.target.result; //alert(event.target.result); } document.getElementById('file').addEventListener('change', onChange); }());
🌐
CodeSandbox
codesandbox.io › s › read-local-json-file-in-javascript-dnly0
read local json file in javascript - CodeSandbox
December 8, 2020 - read local json file in javascript by officialrajdeepsingh
Published   Dec 02, 2020
Author   officialrajdeepsingh
🌐
Edureka Community
edureka.co › home › community › categories › web development › java-script › how to read an external local json file in...
How to read an external local JSON file in JavaScript | Edureka Community
September 3, 2020 - I have saved a JSON file in my local system and created a JavaScript file in order to read the ... the JSON file and print the data in JavaScript?