I am writing two REST APIs which are as following:
- To store the weight of user identified by id
POST /user/weight/{id}
//request body
{ "weight": 125 }
- To get the weight of user identified by id
GET /user/weight/{id}
//response
{ "weight": 125 }
I just want to store these user weight data in memory for now:
I am using following JSON design for this.
// For POST /user/weight/21
// { "weight" : 130 }
{
"21" : { "weight" : 130 }
}
I am using id as the key for JSON so that I can get the weight of the user by doing just data[id], the other alternative is to use array
[
{"id" : 21 , "weight" : 130 }
]
But for this approach I need to do a linear search over the array.
My doubt is which approach is better? Am I abusing the JSON property in the first approach?
If I store this data in database in future which approach has less changes?