Akash Sateesh
Akash Sateesh

Reputation: 812

Read a JSON file in react.js

I have to read JSON file using React.js which is stored in local drive in Mac. I tried using fetch() api but I get data undefined. Heres my code.

fetch('http://localhost:300/107k.json')
    .then(req => console.log(req))
    .then(data => console.log(data))

I am getting data as undefined. How will I be able to read JSON file in react.js?

Upvotes: 4

Views: 9017

Answers (2)

sxzhao
sxzhao

Reputation: 126

The json() method of the Body mixin takes a Response stream and reads it to completion. It returns a promise that resolves with the result of parsing the body text as JSON.

fetch('http://localhost:300/107k.json')
  .then(res => res.json())
  .then(data => console.log(data))

Upvotes: 5

Mμ.
Mμ.

Reputation: 8542

Just use import instead

import react from 'react'

//... rest of imports

import MyJson from '../path/to/json/107k.json';

Upvotes: 12

Related Questions