I'm trying to read a big json file with JS
const jsonFile = JSON.parse("3BP-OOP-PFR.json");
console.log(jsonFile);
The problem is
VM334:1 Uncaught SyntaxError: Unexpected token B in JSON at position 1 at JSON.parse () at main.js:1:23
I'm trying to read a big json file with JS
const jsonFile = JSON.parse("3BP-OOP-PFR.json");
console.log(jsonFile);
The problem is
VM334:1 Uncaught SyntaxError: Unexpected token B in JSON at position 1 at JSON.parse () at main.js:1:23
You are parsing the filename as JSON, which will fail. The JSON.parse
function expects a JSON string value to parse, not a file.
Have you tried importing the file instead?
import jsonFile from "3BP-OOP-PFR.json";
console.log(jsonFile);
If you are using a browser, you will need to fetch the file:
fetch('https://website.com/subdir/3BP-OOP-PFR.json')
.then(res => res.json())
.then(json => console.log(json))
If you want to use async
/await
:
const res = await fetch('https://website.com/subdir/3BP-OOP-PFR.json')
const json = await res.json();
console.log(json);
Keep-in-mind that if you are fetching a JSON file locally, this will fail. Make sure you are using a webserver and running off of localhost
. I believe there are flags that you can pass (CLI) to the browser to override the local file loading error.