How to load local JSON file into variable with JavaScript?

Sometimes, we want to load local JSON file into variable with JavaScript.

In this article, we’ll look at how to load local JSON file into variable with JavaScript.

How to load local JSON file into variable with JavaScript?

To load local JSON file into variable with JavaScript, we can import the JSON file with import.

For instance, we write

{
  "name": "testing"
}

in example.json.

Then we import example.json and get the name property in the JSON object by writing

import * as data from "./example.json";

const { name } = data;

Conclusion

To load local JSON file into variable with JavaScript, we can import the JSON file with import.