0

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

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

I did vote positive for you, but I think whoever did that was because the output of your snippet doesn't print "python" (I don't know why but I'm looking for the reason).
@Rodolfo BocaneGra ok i have edited please check if still there are problems?
No more problems, you did it; I haven't seen really well the array validation, thank you!
0

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)
}
}

1 Comment

obj.properties? Maybe you meant Object.keys(obj)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.