I'm making a little app at Nodejs and I'm struggling trying to loop an irregular JSON to print its data.
My JSON has the next structure:
{
"courses": [
{
"java": [
{ "attendees": 43 },
{ "subject": "Crash course" }
]
},
{
"python":
{
"occurrences": [
{ "attendees": 24 },
{ "subject": "another crash course" },
{ "notes": "completed with issues" }
,
{ "attendees": 30 },
{ "subject": "another crash course" },
{ "notes": "completed with issues" }
]
}
}
],
"instructors":[
{
"John Doe":[
{ "hours": 20 },
{ "experience": 50 },
{ "completed": true }
]
},
{
"Anne Baes": [
{ "hours": 45 },
{ "experience": 40 },
{ "completed": false},
{ "prevExperience": true}
]
}
]
}
What I want to do is to print all the data contained in the JSON (I would like something like):
courses
Java
attendees = 43
...
Anne Baes
hours = 45
experience = 40
completed = false
prevExperience = true
I've tried with:
for(element in data){
console.log(`element = ${{element}}`);
}
and it only prints:
element = [object Object]
element = [object Object]
(which makes sense because the json is made up of two elements )
I've tried nesting the line:
for(element in data){
the problem here is that there is an irregular structure, I mean, "java" and "python" are the same level data but at the same time they have different (array and object) type of value and at the case of 'instructors' they have the same type of value but they're different about them number of values.
Could somebody please help me?:(