Categories
JavaScript Answers

How to load local JSON file into variable with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *