How can I use JavaScript to load a JSON file from my desktop? Alternatively, for a more direct approach:

16 Replies, 746 Views

Protip: If you're using VSCode, the "Live Server" extension makes this trivial. Just right-click your HTML file and hit "Open with Live Server." Then fetch() will work with relative paths.

Otherwise, yeah, FileReader is the way. Here's a async version:

```js
async function loadJSON(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(JSON.parse(reader.result));
reader.onerror = reject;
reader.readAsText(file);
});
}
```

Messages In This Thread



Users browsing this thread: 1 Guest(s)