Is your json
file hosted on a server somewhere? If so, are you getting any errors when you attempt to fetch it?
If the json
file is simply a file on your computer, you won't be able to load it like that. If you are using webpack
or another package manager to bundle your files, you can use import
to import the file into your javascript file like so:
import json from "file.json";
console.log("My json: ", json);
// Rest of javascript file here
This assumes that you've set up your bundler to handle json
files though. Webpack does this for you automatically. So if you're using it, then this should work. Not sure about other bundlers though.
But it's best to host the file on a server and fetch it, especially if the data changes constantly - such as from an API.