Answer the question
In order to leave comments, you need to log in
What should be the nesting of returned data in a RESTful API?
There is a standard data structure:
user_id
user_name
user_...
-----
group_id
group_user
group_name
group_...
-----
item_id
item_group
item_name
item_...
items : [
{id : 1, name : 'Name 1', group : 1},
{id : 2, name : 'Name 2', group : 1},
{id : 3, name : 'Name 3', group : 2},
{id : 4, name : 'Name 4', group : 3}
]
oritems : [
{id : 1, name : 'Name 1', group : 1, user: 1},
{id : 2, name : 'Name 2', group : 1, user: 1},
{id : 3, name : 'Name 3', group : 2, user: 1},
{id : 4, name : 'Name 4', group : 3, user: 2}
]
oritems : [
{id : 1, name : 'Name 1', group : {id : 1, name: 'Group 1', user: 1}},
{id : 2, name : 'Name 2', group : {id : 1, name: 'Group 1', user: 1}},
{id : 3, name : 'Name 3', group : {id : 2, name: 'Group 2', user: 1}},
{id : 4, name : 'Name 4', group : {id : 3, name: 'Group 3', user: 2}}
]
oritems : [
{id : 1, name : 'Name 1', group : {id : 1, name: 'Group 1', user: {id: 1, name: 'User 1'}}},
{id : 2, name : 'Name 2', group : {id : 1, name: 'Group 1', user: {id: 1, name: 'User 1'}}},
{id : 3, name : 'Name 3', group : {id : 2, name: 'Group 2', user: {id: 1, name: 'User 1'}}},
{id : 4, name : 'Name 4', group : {id : 3, name: 'Group 3', user: {id: 2, name: 'User 2'}}}
]
or{
items : [
{id : 1, name : 'Name 1', group : 1},
{id : 2, name : 'Name 2', group : 1},
{id : 3, name : 'Name 3', group : 2},
{id : 4, name : 'Name 4', group : 3}
],
groups : [
{id: 1, name: 'Group 1', user: 1},
{id: 2, name: 'Group 2', user: 1},
{id: 3, name: 'Group 3', user: 2}
],
users : [
{id: 1, name: 'User 1'},
{id: 2, name: 'User 2'}
]
}
Answer the question
In order to leave comments, you need to log in
Everything, absolutely everything, for which there is an ID, should be loaded separately (a general request for metadata to the server during initialization, including the necessary ones in the object in the form of a generalized table ... a sea of options). At the front, they will figure out how to put it together.
Otherwise, you will get a huge overhead for data adaptation and non-obvious data sets. Moreover, in the case of updating data in real time, you will have to dig up already received objects and recalculate something (as you're lucky here).
You need to proceed from the fact that:
1. The programmer using your API should be as clear as possible. This is the main thing.
2. Speed is important, but it's secondary to clarity.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question