I have a Wine entity which is related to a country in a one to many relation (one country - many wines)
I can think of two ways to represent this in a json web service:
One is just including the whole country in a json
{
id: 76,
code: "FR",
name: "France"
}
{
id: 79,
name: "Bordeaux",
country: {
id: 76,
code: "FR",
name: "France"
},
year: "2005"
}
the other is including each country property to the same level, with a prefix
{
id: 79,
name: "Bordeaux",
countryId: 76,
countrCode: "FR",
countryName: "France"
year: "2005"
}
In any case, when updating or creating a new wine, I will just save the country id, I won't allow to modify a country from the endpoint of wines
The second approach would be easier to implement (I would have a join view on the db, and the serializer would just transform every field to json), but I think the first one is more elegant
Just wanted to know if there's any standard for this kind of situation and in case there isn't what is the more common approach...
ps: I don't mean to start a debate on the "restfullness" of the solution, just looking for a smart an simple way to handle this relation...
In your case, country is small enough to consider nesting it every time. This is not always the case, though, and there is no standard that I've found on handling this.
I've done this with a custom reference type:
{
"id": 79,
"name": "Bordeaux",
"country": {
"ref": {
"id": 76,
"uri": "/country/76"
}
},
"year": "2005" }
Some helpful ideas here as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With