8

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...

opensas
  • 60,462
  • 79
  • 252
  • 386

2 Answers2

6

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.

Roy Truelove
  • 22,016
  • 18
  • 111
  • 153
  • Thx opensas - there was quite a bit missing, actually. Linted it up. – Roy Truelove Oct 10 '12 at 12:57
  • I implemented something similar, but wihtout the ref, here's it is running on openshift: http://ideas-jugar.rhcloud.com/api/ideas, and the git repo: https://github.com/RestOpenGov/ideas-ba/tree/master/webservice, so is the ref necessary at all? – opensas Oct 10 '12 at 13:17
  • I didn't see any foreign references in your example, only nested objects and self references – Roy Truelove Oct 10 '12 at 13:26
  • I only implemented it, experimentally, in the idea entity, see the url property, but the idea is to have nested objects, each of them with a url property – opensas Oct 10 '12 at 19:08
-5

JSon isn't there to have Foreign Keys. That's something that is related to RDBMS. In my opinion, JSon should show everything nested in itself.

MongoDB fortifies this opinion. IN MongoDB, you store everything as a JSon and the "FKs" are included in the same document. If you need FKs, then you don't need mongo and you don't need JSon in my opinion.

BTW, if you will always show the country, it doesn't make sense to make always two requestsac

mgonto
  • 6,605
  • 2
  • 29
  • 36
  • 7
    This answer ignores the fact that we are talking about a RESTful web service that's using JSON to serialise, and so will need to represent relations between objects. – Daniel Watkins Oct 17 '12 at 08:45
  • @DanielWatkins +1 agree, there is no impedance mismatch between JSON representation of data objects and RDBMs – Morten Jensen Nov 20 '12 at 13:59