// example.json
{
"name": "testing"
}
// ES6/ES2015
// app.js
import * as data from './example.json';
const {name} = data;
console.log(name); // output 'testing'
<script type="module">
import data from "./data.json" assert { type: "json" };
console.log(data);
</script>
// also add this=> type:"module" in json file as object
import countryTable from "./data/countries.json" assert { type: "json" };
const jsonCountry = "dist.countries.json";
fetch(jsonCountry)
.then(Response => Response.json())
.then(data => {
console.log(data);
// or whatever you wanna do with the data
});
Using fetch function
Code to access employees.json using fetch function −
fetch("./employees.json")
.then(response => {
return response.json();
})
.then(data => console.log(data));