Reputation: 332
I was considering, in terms of development, code quality or simply standards, which is the best way to structure the information in a JSON when considering , for instance, managing the same object on a @GET or @PUT method.
Please check the example:
Imagine an API which has a @GET method which returns a set of data, and this set of data contains a category:
{....
"category":{
"id": 1,
"name":"categoryName"
},
...}
As you can see, cathegory is defined as an object with an ID and a name. This information is sent to the front end obviously with UI purposes. Nevertheless, if the frontend modifies this object and does a @PUT in order to update the data in the backend, the cathegory is specified by:
{....
"category":1,
...}
As you can see there, the structure of the category has been changed.
Which is the correct/proper/best way to handle this, to keep the same structure (as it is on the first snippet) or consider having another one (as on the second snippet) which, for instance, on a typed front end programming language leads to the creation of new objects/attributes.
Upvotes: -2
Views: 804
Reputation: 2947
Your Category
object should/will always retain the same structure, no matter what method you use.
The following is a pseudo method that will return a list of all Categories
GET /categories
Response:
[
{
"id": 1,
"name": "Something",
"special": true
},
{
"id": 2,
"name": "Another",
"special": false
}
]
Now, if you want to update a specific Category
PUT /categories/{id}
Body
{
"id": 1, //Technically, this is an optional field
"name": "Something has changed",
"special": false
}
As you can see, the structure is the same - which is what you should strive for.
Upvotes: 1