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

12
  • 1
    How your config is setup? Do you use webpack? Commented Dec 4, 2018 at 15:07
  • The import expects a JavaScript source file, and JSON is not that. Commented Dec 4, 2018 at 15:07
  • @MehiShokri Yeah i am using webpack. Commented Dec 4, 2018 at 15:08
  • @Pointy What's wrong with importing JSON? Commented Dec 4, 2018 at 15:09
  • which version ? Commented Dec 4, 2018 at 15:11

4 Answers 4

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!

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

Comments

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;

Comments

-1

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

3 Comments

json-loader is loaded by default
Only since version 2 it is :-)
He mentioned it's 4.7
-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

2 Comments

I am using webpack version 4.26.1
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.

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.