I am in need of a function called ** getJson () ** that can fetch and return data from a json file.
Here is the JSON data (file.json):
[
{
"01/01/2021":"Confraternização Universal",
"15/02/2021":"Carnaval",
"16/02/2021":"Carnaval",
"02/04/2021":"Paixão de Cristo",
"21/04/2021":"Tiradentes",
"01/05/2021":"Dia do Trabalho",
"03/06/2021":"Corpus Christi",
"07/09/2021":"Independência do Brasil",
"12/10/2021":"Nossa Sr.a Aparecida - Padroeira do Brasil",
"02/11/2021":"Finados",
"15/11/2021":"Proclamação da República",
"25/12/2021":"Natal"
}
]
- I have attempted to use an async function, but it did not work as expected.
The code I used is:
async function getJson() {
const response = await fetch('file.json');
const data = await response.json();
return data;
}
console.log(getJson());
Result:
Promise {<pending>}
- It would be beneficial if the fetched data could be stored in a variable like ** obj **. I tried the following code, but encountered a problem.
The code I tried is:
var obj;
async function getJson() {
const response = await fetch('file.json');
obj = await response.json();
}
console.log(obj);
Result:
undefinded