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

16 Replies, 745 Views

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!

Messages In This Thread



Users browsing this thread: 1 Guest(s)