so heres what happens,
i made a function that reads my JSON file, and at the end returns the value found
here are how my files are organized my files
and here is my script.js file
async function readJSONFile(path) {
const [filePath, jsonPath] = path.split('|');
try {
const response = await fetch(filePath);
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
}
const jsonData = await response.json();
const keys = jsonPath.split('/');
let value = jsonData;
for (const key of keys) {
if (Array.isArray(value) && !isNaN(key)) {
value = value[parseInt(key)];
} else {
value = value[key];
}
if (value === undefined || value === null) {
return undefined;
}
}
return value;
} catch (error) {
console.error(`Failed to read JSON file: ${error}`);// theres an error
return undefined;
}
}
const path = 'Items.json|Data/taxValue';
readJSONFile(path).then(result => console.log(result)); // Should print 1.15
as well as my JSON file if needed
{
"Data": [
{
"taxValue": 1.15
}
],
"Fruits": [
{
"name": "apple",
"cost": 1.59,
"tax": true,
"itemId": 12345,
"ect...": 0
}
],
"Others": [
]
}
i was expecting to read the file but, i got an error,
‘script.js:25 Failed to read JSON file: TypeError: Failed to fetch
‘,
not sure how to fix, thats why im sking for help.