I am facing an issue while trying to extract and manipulate JSON data from a file for an application I am developing. When looping through the data, I encounter an undefined error that seems to indicate a missing property in the JSON object when accessing it with a loop variable. However, if I directly index the JSON array with a hardcoded number, the property loads correctly. I need assistance in resolving this issue. Below is a snippet of the code along with the JSON data:
I have attempted various methods like stringifying and parsing the JSON data again, as well as trying both square brackets and dot notation for access, but all lead to the same problem.
Code snippet to access the JSON data:
import ontology from '../../data/ontology.json'
const totalAnswerList = ontology.answers
for (var i = 0; i <= totalAnswerList.length; i++) {
var wordID = totalAnswerList[i] // wordID.id returns undefined
var wordID2 = totalAnswerList[0] // wordID2.id works
alert(JSON.stringify(wordID) + JSON.stringify(wordID2) + '\nWord ID hardcoded: ' + wordID2.id)
}
//ontology.json
{
"answers": [
{
"id": "examination",
"category_id": "examination",
"synonyms": ["examination"]
}, ...
], ...
}