Accepted answer

One nice way (without adding a fake .js extension which is for code not for data and configs) is to use json-loader module. If you have used create-react-app to scaffold your project, the module is already included, you just need to import your json:

import Profile from './components/profile';

This answer explains more.


This worked well in React 16.11.0

// in customData.js
export const customData = {
  //json data here
  name: 'John Smith',
  imgURL: '',
  hobbyList: ['coding', 'writing', 'skiing']
// in index.js
import { customData } from './customData';

// example usage later in index.js


Something that worked for me was to simply place the JSON file in the public folder. You can simply import in any js using


It is as simple as that I guess. Definitely worth a try :D


var langs={

Require it in your module:

let langs=require('./languages');



In current react build you simply import and use:

import jsonData from 'path/to/myJson.json'


there are multiple ways to do this without using any third-party code or libraries (the recommended way).

1st STATIC WAY: create a .json file then import it in your react component example

my file name is "example.json"

{"example" : "my text"}

the example key inside the example.json can be anything just keep in mind to use double quotes to prevent future issues.

How to import in react component

import myJson from "jsonlocation";

and you can use it anywhere like this


now there are a few things to consider. With this method, you are forced to declare your import at the top of the page and cannot dynamically import anything.

Now, what about if we want to dynamically import the JSON data? example a multi-language support website?


1st declare your JSON file exactly like my example above

but this time we are importing the data differently.

let language = require('./en.json');

this can access the same way.

but wait where is the dynamic load?

here is how to load the JSON dynamically

let language = require(`./${variable}.json`);

now make sure all your JSON files are within the same directory

here you can use the JSON the same way as the first example


what changed? the way we import because it is the only thing we really need.

I hope this helps.


try with export default DATA or module.exports = DATA


// rename the .json file to .js and keep in src folder

Declare the json object as a variable

var customData = {

Export it using module.exports

module.exports = customData;

From the component that needs it, make sure to back out two folders deep

import customData from '../customData';


React 17 created from create-react-app, importing json just work by default.

import config from './config.json'


Please store your JSON file with the .js extension and make sure that your JSON should be in same directory.


The solution that worked for me is that:- I moved my data.json file from src to public directory. Then used fetch API to fetch the file

fetch('./data.json').then(response => {
      return response.json();
    }).then(data => {
      // Work with JSON data here
    }).catch(err => {
      // Do something for an error here
      console.log("Error Reading data " + err);

The problem was that after compiling react app the fetch request looks for the file at URL "http://localhost:3000/data.json" which is actually the public directory of my react app. But unfortunately while compiling react app data.json file is not moved from src to public directory. So we have to explicitly move data.json file from src to public directory.


Simplest approach is following

// Save this as someJson.js
const someJson = {
  name: 'Name',
  age: 20

export default someJson


import someJson from './someJson'


With json-loader installed, you can use

import customData from '../customData.json';

or also, even more simply

import customData from '../customData';

To install json-loader

npm install --save-dev json-loader


This old chestnut...

In short, you should be using require and letting node handle the parsing as part of the require call, not outsourcing it to a 3rd party module. You should also be taking care that your configs are bulletproof, which means you should check the returned data carefully.

But for brevity's sake, consider the following example:

For Example, let's say I have a config file 'admins.json' in the root of my app containing the following:

  "userName": "tech1337",
  "passSalted": "xxxxxxxxxxxx"

Note the quoted keys, "userName", "passSalted"!

I can do the following and get the data out of the file with ease.

let admins = require('~/app/admins.json');

Now the data is in and can be used as a regular (or array of) object.

Related Query

More Query from same tag