Author avatar

Gaurav Singhal

Load a JSON File with ES6 Modules Implementation

Gaurav Singhal

  • Sep 29, 2020
  • 4 Min read
  • Sep 29, 2020
  • 4 Min read
Web Development
Front End Web Development
Client-side Frameworks


ES6 is a common standard for JavaScript; it’s also called ECMAScript 2015, with the first version released in 2011. ES6 adds extensive features to make JavaScript app development more accessible. Most JavaScript-based frameworks, libraries, and the vanilla JavaScript app follow the ECMAScript standard. During web development, accessing JSON files is an ordinary operation, and consuming JSON files is allowed using ES6 implementation.

In this guide, you will learn how to access or consume any JSON file by implementing the ES6 standard.

Import JSON File Using NPM Package

JSON files contain a key-value pair, and there is a parent key that represents the parent node of the JSON file.

In React, if you want to load the JSON file without using the existing webpack settings, can make use of a third-party NPM package. One popular package is json-loader.

1npm install json-loader

In this guide, we will be using sample JSON data stored in the Data.json file.

2  "data": {
3    "category1": {
4      "name": "test123",
5      "title": "this is category 123"
6    },
7    "category2": {
8      "name": "test456",
9      "title": "this is category 456"
10    },
11    "category3": {
12      "name": "test789",
13      "title": "this is category 789"
14    }
15  }

After the above JSON file is created, it will be available to import and use into existing React components.

1import React, { Component } from "react";
2// Custom JSON file
3import Data from "./data";
5export class ImportJsonFile extends Component {
6    render() {
7        console.log(Data) // will print the JSON file content
8        return (
9            <>
10                <div>
11                    // Use JSON data here
12                </div>
13            </>
14        );
15    }
18export default ImportJsonFile;

Notice the ES6 import statement that imports the local JSON file, which you created previously. Using the ES6 import statement along with the json-loader, any JSON file can be consumed into the React app.

Import JSON File Using Webpack Config

The basic webpack config rule with the json5-loader is configured as defined below.

If your React app uses a webpack config version > 2.0.0, then there is no need to install json-loader because webpack uses json5-loader by default when its configured with the React app.

Note: webpack.config.js is sourced from here .

1module.exports = {
2  module: {
3    rules: [
4      {
5        test: /\.json5$/i,
6        loader: 'json5-loader',
7        type: 'javascript/auto',
8      },
9    ],
10  },

In the webpack config file, the loader rule accepts the NPM package to load any of the JSON files in your app. One package used is called json5-loader.

Along with the loader rule, there are two other rules, one of which is test.

1test: /\.json5$/i,

That identifies the file format of the JSON or gives a warning at the time of compilation.

There is an additional Boolean option available called esModule, which is used to define whether the module should be ECMAScript-supported or not.

The basic setting for esModule is shown below.

1module.exports = {
2  module: {
3    rules: [
4      {
5        test: /\.json5$/i,
6        loader: 'json5-loader',
7        options: {
8          esModule: true,
9        },
10        type: 'javascript/auto',
11      },
12    ],
13  },

By maintaining the rules with a webpack, the React app is configured to access the JSON file from the physical app location by verifying its file extension and the default JSON loading library.


React app uses various ES6 features, such as let, const, spread operator, arrow function, class, and literals, so your app should be well configured to support the JSON file format.

Webpack configuration is the right approach to allow JSON files to be imported into a React app with ES6 implementation Try configuring webpack on your own to access the JSON files.