简体   繁体   中英

How to iterate over an irregular nested json with node js

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?:(

You can do that using recursion and for..in loop

 const obj = { "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} ] } ] }; function print(obj,isArr = false){ for(let key in obj){ if(typeof obj[key] === 'object'){ if(isArr === false) console.log(key) print(obj[key],Array.isArray(obj[key])); } else console.log(`${key} = ${obj[key]}`) } } print(obj) 

You can try iterating over the values using a recursive function like this

var Obj {

//Contents

}

function printRec(obj){
if(Object.keys(obj).length>0||Array.isArray(obj)){
  for(elem in obj){
  printRec(obj);
  }
}else{
  //Process Value
  console.log(obj)
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM