fetch a json file javascript code example

Example 1: fetch json file

fetch('http://example.com/movies.json')
  .then(response => response.json())
  .then(data => console.log(data));

Example 2: how to fetch local json file in javascript

Try to use file system. Don't think reading from a JSON file works like that.

const fs = require('fs');
const json_data = require('../services/contributors.JSON');

fs.readFile(json_data, 'utf8', function (err, data) {
  try {
    data = JSON.parse(data)
    for (let i in data){
    console.log('Name:',data[i].name)
    }
  } catch (e) {
    // Catch error in case file doesn't exist or isn't valid JSON
  }
});