0

I'm using react and working on getting json files in the component.

I want to get the input.json file using fetch, but it's not data in the console. Why is this and how do I get the data?

input.json

{
    "formId": 1,
    "title": "from",
    "items": [{
      "itemId": 1,
      "title": "style",
      "formType": 1,
      "options": [{
        "id": 1,
        "text": "one"
      }, {
        "id": 2,
        "text": "two"
      }, {
        "id": 3,
        "text": "three"
      }]
    }

App.js

import React from 'react';
import './App.css';
import inputData from './assets/input.json';

function App() {

  const getData = () => {
    fetch(inputData).then(response => {
               return response;
             }).then(data => {
               console.log(data);
             }).catch(err => {
               console.log(err)
             });
  }

  getData();

  return (
    <div className="App">
      <h2>Fetch</h2>
    </div>
  );
}

export default App;

console.logenter image description here

4
  • please share your all code Commented Sep 23, 2019 at 12:57
  • How you exported your json in input.json file, as default export? Commented Sep 23, 2019 at 13:51
  • Possible duplicate of How to read JSON file with fetch() in javascript? Commented Sep 23, 2019 at 13:56
  • I'll check. The problem seems to be that json file is not being exported. Thank you for your answer. Commented Sep 23, 2019 at 14:12

3 Answers 3

1

you are missing something in your result fetching, it should be

 fetch(inputData).then(response => {
            response.json().then(data => {
            console.log(data);
          }).catch(err => {
            console.log(err)
          });


Sign up to request clarification or add additional context in comments.

Comments

0

Did you try with parsing response to json format?

something like this:


import inputData from '../assets/input';

const getData = () => {
 fetch(inputData).then(response => {
const result = response.json()
            return result;
          }).then(data => {
            console.log(data);
          }).catch(err => {
            console.log(err)
          });
}

1 Comment

An error occurs when you perform response.json() because it is already a json file. Thank you for your answer!
0

Try this,

import React from 'react';
import './App.css';
import inputData from './assets/input.json';

function App() {

  const getData = () => {
    fetch(inputData).then(response => {
                console.log(response);
                console.log(response.data);
              }) catch (err => console.log("err",err))
  }

  getData();

  return (
    <div className="App">
      <h2>Fetch</h2>
    </div>
  );
}

export default App;

1 Comment

Thank you for your answer. But it's the same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.