When I am designing applications, especially if I have to transfer data to other services, this is the approach that I came up with:
Define the structure
what is the format I want to use?
How do I make it more extensible?
Nested objects
“name”: “sampleUni”,
“offeredCourses”: {
“SMPL101”: {
“courseName”: “Intro to Sample Course 101”,
“enrolledStudents”: {
“1”: {
“name”: “John Doe”,
“age”: 23
},
“2”: {
“name”: “Apple Smith”,
“age”: 20
}
}
}
}
}
Advantages of nested json objects
intuitive and may be easier to understand
better encapsulation
Disadvantages of nested json objects
Deep nested objects are complex and data parsing/modification could get more difficult
Advantages of flatter json objects
simpler structure => good for simple data
faster data access/retrieval => less nested levels to traverse.
Disadvantages of flatter json objects
could result in more ambiguity
the relationship might not be as clear as the nested representation
Ultimately need to decide in conjunction with what I want to do with the data, think more about future changes, use cases with focus on OOP.