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

16 Replies, 526 Views

"Best way to load a JSON file from desktop using JavaScript?"

Hey folks!

I'm trying to use javascript load json file from desktop for a small project, but I'm stuck. What's the easiest way to do this?

I tried `fetch()` but it’s not working—probably because of file permissions or something?

Any tips or code snippets would be awesome. Thanks in advance!

---

*Or the troubleshooting version:*

"Why isn't my javascript load json file from desktop working?"

So I’ve got this JSON file on my desktop, and my code looks like:

```js
fetch('file.json').then(res => res.json())...
```

But it’s throwing errors. Am I missing something obvious?

Is there a better way to do this? Help a noob out! 😅
Hey! Yeah, fetch() won't work directly for local files due to browser security restrictions.

If you're just testing, try running a local server like `live-server` (install via npm) or use Python's `python -m http.server`.

Otherwise, you can use the File API to let users manually select the JSON file from their desktop:

```js
const input = document.createElement('input');
input.type = 'file';
input.accept = '.json';
input.onchange = e => {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = event => console.log(JSON.parse(event.target.result));
reader.readAsText(file);
};
input.click();
```

Hope that helps!
Bro, browsers block javascript load json file from desktop for security reasons. You gotta either:

- Drag & drop the file into the browser (kinda hacky but works for quick tests)
- Use Node.js if you're okay with running it outside the browser (`fs.readFileSync('path/to/file.json')`)
- Or like others said, spin up a local server.

Also, check if your JSON is valid—sometimes errors come from malformed files.
For a quick fix, you can use the `--allow-file-access-from-files` flag in Chrome, but it's deprecated and messy.

Better long-term solution: Use the FileReader API or a local dev server. VSCode's Live Server extension is super easy for this.

Here's a clean FileReader example:

```js
document.querySelector('#file-input').addEventListener('change', (e) => {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = (event) => {
const data = JSON.parse(event.target.result);
console.log(data);
};
reader.readAsText(file);
});
```
Wait, are you trying to do this in a browser or Node.js?

For browser: You can't directly javascript load json file from desktop without user interaction (like file input). Security thing.

For Node.js:

```js
const fs = require('fs');
const data = JSON.parse(fs.readFileSync('/path/to/your/file.json'));
```

If you're stuck, share the exact error—might be a path issue!
Hey, OP here! Wow, thanks for all the replies—didn’t expect so many solutions!

I tried the FileReader approach and it worked perfectly. Didn’t realize browsers had those security limits for local files.

Quick follow-up: If I *do* use a local server (like Live Server), can I then use fetch() with relative paths, or do I still need full paths?

Thanks again, y’all are legends! 😊
If you're just playing around, try JSON.parse() on a hardcoded string first to rule out JSON syntax errors.

For actual file loading, the File API is your friend. Here's a minimal example:

```html
<input type="file" id="jsonFile" accept=".json">
<script>
document.getElementById('jsonFile').addEventListener('change', function(e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = () => console.log(JSON.parse(reader.result));
reader.readAsText(file);
});
</script>
```

No server needed!
Man, I struggled with this too! fetch() won't cut it for local files.

Quick workaround: Use `require()` if you're in a Node environment:

```js
const data = require('./file.json');
```

For browsers, you *have* to use FileReader or a server. Mozilla’s docs have a great guide on the File API—check it out!
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);
});
}
```



Users browsing this thread: 1 Guest(s)