0

i got this error

VM305:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

and I don't know why. My json data:

[
  {
   "id": 1,
    "title": "Avatar",
    "distributor": "20th Century Fox",
    "year": 2009,
    "amount": "$2,787,965,087",
    "img": {
      "src": "media/avatar.jpg",
      "alt": "avatar"
    },
    "ranking": 1
  },
  {
    "id": 2,
    "title": "Titanic",
    "distributor": "20th Century Fox",
    "year": 1997,
    "amount": "$2,187,463,944",
    "img": {
      "src": "media/titanic.jpg",
      "alt": "titanic"
    },
    "ranking": 2
  },
  {
    "id": 3,
    "title": "Star Wars: The Force Awakens",
    "distributor": "Walt Disney Studios Motion Pictures",
    "year": 2015,
    "amount": "$2,068,223,624",
    "img": {
      "src": "media/star_wars_the_force_awakens.jpg",
      "alt": "star_wars_the_force_awakens"
    },
    "ranking": 3
  },
  {
    "id": 4,
    "title": "Avengers: Infinity War",
    "distributor": "Walt Disney Studios Motion Pictures",
    "year": 2018,
    "amount": "$2,048,359,754",
    "img": {
      "src": "media/avengers_infinity_war.jpg",
      "alt": "avengers_infinity_war"
    },
    "ranking": 4
  },
  {
    "id": 5,
    "title": "Jurassic World",
    "distributor": "Universal Pictures",
    "year": 2015,
    "amount": "$1,671,713,208",
    "img": {
      "src": "media/jurassic_world.jpg",
      "alt": "jurassic_world"
    },
    "ranking": 5
  }
]
class List extends Component {
  constructor() {
    super();
    this.state = {
      data: [],
      loading: true,
    };
  }

  async componentDidMount() {
    
    const movies = await fetch('../../assets/data.json');
    const moviesJSON = await movies.json();
  
    console.log(moviesJSON);
    if (moviesJSON) {
      this.setState({
        data: moviesJSON,
        loading: false,
      });
    }
  }

  render() {
    const { data, loading } = this.state;

    if (loading) {
      return <div>Loading...</div>;
    }

    return (
      <div className='row'>
        {data.map(movie => (
          <div key={movie.id} className='col-sm-2'>
            <Card movie={movie} />
          </div>
        ))}
      </div>
    );
  }
}

export default List;
0

3 Answers 3

2

The URL where you are fetching the json is almost certainly not returning the json file that you think it is, but instead serving an HTML error page (which starts with <). Add a console.log(movies) after the fetch to debug this.

Also, the URL you have is a path (including ..), not a URL. The question is where that file is served by your web server.

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

1 Comment

Agreed Christian.
1
const movies = await fetch('../../assets/data.json');

instead of doing this, you should import json at the top of your file

import movies from '../../assets/data.json';

Then you can access movies in ComponentDidMount. Since it is local data, you don't need to make an api call

Comments

1

You get this error when fetch doesn't return valid json data. This is the line causing the error.

const moviesJSON = await movies.json();

You can try to change movies.json() to movies.text() and log to see the data.

const moviesJSON = await movies.text();
console.log((moviesJSON));

Comments

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.