You can use jQuery .getJSON() function:

$.getJSON('http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D%27WRC%27&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback', function(data) {
    // JSON result in `data` variable
});

If you don't want to use jQuery you should look at this answer for pure JS solution.

Answer from Dan Barzilay on Stack Overflow
🌐
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 - The only difference would be the URL. For example, suppose we have a local file within our project's folder named data.json that contains the following JSON data: <!--./data.JSON--> { "id": 1, "title": "Hello World", "completed": false } We can now read this file in JavaScript using the Fetch API method:
🌐
Educative
educative.io › answers › how-to-read-a-json-file-from-a-url-in-javascript
How to read a JSON file from a URL in JavaScript
An optional parameter that we sometimes ... loadJSON("https://jsonplaceholder.typicode.com/posts",myData,'jsonp'); Define the myData function. When data is read from the website, it is passed directly to the function myData...
🌐
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
🌐
Oxylabs
oxylabs.io › blog › javascript-read-json-file
How to Read JSON Files in JavaScript: Tutorial
Using the Fetch API method, this JavaScript function retrieves a JSON file from a given URL. If the network response is successful, it parses data into a JSON string and displays the results using the displayProducts() method.
🌐
ZetCode
zetcode.com › javascript › jsonurl
JavaScript JSON from URL - Fetching Data Explained
Learn how to fetch JSON data from a URL in JavaScript using Fetch API, JQuery, and XMLHttpRequest, with examples and explanations.
🌐
ReqBin
reqbin.com › code › javascript › wc3qbk0b › javascript-fetch-json-example
How do I fetch JSON using JavaScript Fetch API?
... fetch('https://reqbin.com/... To automatically parse a JSON response into a JavaScript object, you need to use the response.json() method....
Find elsewhere
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript read json file from url
JavaScript read JSON file from URL
June 1, 2023 - To read a JSON file from a URL using JavaScript, you can use the XMLHttpRequest or fetch API. Here's an example using both methods...
🌐
GitHub
gist.github.com › tomasevich › 5e42a97cab516f0792d46f68cd941c20
node js: read JSON file from URL like object · GitHub
var request = require ('request') request ({ url: 'https://raw.githubusercontent.com/rateljs/plugin/master/package.json', json: true }, (error, response, body) => { !error && response.statusCode === 200 ?
🌐
YouTube
youtube.com › watch
Web Basics - Accessing JSON data from URL - YouTube
This video shows how to access JSON data from a URL using JS and the JQuery resource
Published   December 13, 2019
🌐
Rayobyte
rayobyte.com › blog › javascript-load-json-file-from-descktop
How to JavaScript Load JSON File From Desktop || Rayobyte
September 23, 2024 - When you do this, the fetch API will go to work. The JavaScript function retrieves the JSON file from the URL you provided to it. In situations where this is successful, the tool will then parse the data into a JSON string and display it.
Top answer
1 of 4
6

After so many months of search, I found the solution. Hence, I am answering my own question.

When JSON is not supported and when we are stuck with Same Origin Policy, we have to wrap around our JSON with a padding and make it a JSONP.

To do that, we have a life saving website http://anyorigin.com/

You can paste your URL and get the corresponding JQuery code something like this,

$.getJSON('http://anyorigin.com/get?url=http%3A//webapp.armadealo.com/home.json&callback=?', function(data){
$('#output').html(data.contents);
});

If you want to use your own code, then just use the URL from the code above, which is

http://anyorigin.com/get?url=http%3A//webapp.armadealo.com/home.json&callback=?

This above URL will give you the same JSON data as JSONP and solves all the trouble.

I had used the following code, which on success calls displayAll function

$.ajax({
        url: 'http://anyorigin.com/get?url=http%3A//webapp.armadealo.com/home.json&callback=?',
        type: 'GET',
        dataType: "json",
        success: displayAll
    });

function displayAll(data){
    alert(data);
}
2 of 4
5

If you look in Chrome inspector, you probably see this error:

XMLHttpRequest cannot load http://webapp.armadealo.com/home.json. Origin http://stackoverflow.com is not allowed by Access-Control-Allow-Origin.

What this means is that the server doesn't want the client web page reading the file. The client isn't trusted. This is a basic security feature of XMLHttpRequest in order to prevent a site like mybank.evil.com from downloading data from mybank.com. It unfortunately makes testing from a local file challenging.

If you trust any site with your data or a select number of sites, you can configure your server script to send the Access-Control-Allow-Origin to allow certain sites through.

See https://developer.mozilla.org/en/http_access_control for more details.

🌐
Delft Stack
delftstack.com › home › howto › javascript › get json from url in javascript
How to Get JSON From URL in JavaScript | Delft Stack
February 2, 2024 - <!DOCTYPE html> <html> <head> <meta ... </body> </html> ... In this case, the fetch method simply takes the URL for allocating the data server and ensures to return the JSON data....
🌐
Linux Tip
linuxscrew.com › home › programming › javascript › how to read a local/remote json file in javascript [examples]
How to Read a Local/Remote JSON File in JavaScript [Examples]
November 28, 2021 - Above, fetch is used to retrieve the file at the given URL. The first then statement retrieves the JSON parsed version of the response. The second then statement then contains the parsed JSON file ready for use – in this case, it’s simply ...
🌐
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 ...
🌐
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.
🌐
CoreUI
coreui.io › answers › how-to-read-a-json-file-in-javascript
How to read a JSON file in JavaScript · CoreUI
December 3, 2025 - async function readJsonFile(filePath) ... error) }) This code uses fetch to retrieve the JSON file from the specified path, then calls the json() method which automatically parses the JSON content....