2

I am trying to load static JSON data to my react app. But, it wan't allow me to load data.

I am using webpack version 4.26.1

It shows me following error:

SyntaxError: src/data/movieData.json: Unexpected token, expected ; (2:10)

  1 | {
  2 |     "data": [
    |           ^
  3 |         {
  4 |           "id": 1,
  5 |           "title": "Freed",

My Code:

data/jsonResponse.json

{
    "data": [
        {
          "id": 1,
          "title": "Freed"
    },
    {
          "id": 2,
          "title": "Fifty"
    }
    ]
}

main.js

import React, { Component } from 'react';
import Content from './Content';
import  jsonResponse from './data/jsonResponse.json';

class Main extends Component {
    render() {
        return (
            <div className="main">
                <Content item={ jsonResponse } />
            </div>
        );
    }
}

export default Main;

Content.js

import React from 'react';

  const Content = () => {
    const movies = this.props.item.data;
      return (
        movies.map(movie => {
            return (
                <span >{movie.title}</span>
            );
         })
        )
}

export default Content;

Edited:

If i use js instead of JSON like:

const movies_data = {
   "data": [
            {
              "id": 1,
              "title": "Freed"
        },
        {
              "id": 2,
              "title": "Fifty"
        }
        ]
    }
export default movies_data;

and in Main.js file

import  jsonResponse from './data/movieData';

Then in browser it shows following error.

Cannot read property 'props' of undefined

ketan
  • 19,129
  • 42
  • 60
  • 98

4 Answers4

6

There are 2 workarounds for loading json files in a js file.

  1. Rename your json file to .js extension and export default your json from there.

  2. Since json-loader is loaded by default on webpack >= v2.0.0 there's no need to change your webpack configs.
    But you need to load your json file as json!./data/jsonResponse.json (pay attention to json!)

EDIT:

Cannot read property 'props' of undefined

The reason you're getting this error is because you're trying to access this on a functional component!

Mehrdad Shokri
  • 1,974
  • 2
  • 29
  • 45
2

Answer regarding edited question and Cannot read property 'props' of undefined error

You can't access this in functional components. props are passed as argument to functional components so please try this (Content.js file):

import React from 'react';

  const Content = (props) => {
    const movies = props.item.data;
      return (
        movies.map(movie => {
            return (
                <span >{movie.title}</span>
            );
         })
        )
}

export default Content;
Bartek Fryzowicz
  • 6,464
  • 18
  • 27
-1

You have to add a JSON loader to import json files.

ehmprah
  • 154
  • 1
  • 3
-1

You need to check if in your Webpack config if exist an loader to JSON.

I recommend to use this loader. https://www.npmjs.com/package/json-loader

  • I am using webpack version 4.26.1 – ketan Dec 04 '18 at 15:20
  • Please provide concrete answer with code snippets. pointing to references per se Does not help. Also, he mentioned he's webpack version is >=2.0.0 so your answer basically does nothing. – Mehrdad Shokri Dec 04 '18 at 17:14