shou2017.com
JP

How to read a JSON file in JavaScript

Sun Nov 15, 2020
Sat Aug 10, 2024

If you want to read a json file locally like this:

{
  id:"1",
  name:"taro"
},
{
  id:"2",
  name:"hanako"
},

Most people use jQuery’s $.getJSON() to read it, but you can easily do it with just JavaScript.

Yes, you can use the Fetch API.

fetch('../index.json')
  .then(response => response.json())
  .then(data => console.log(data));
See Also