tags:

views:

93

answers:

2

I am designing a RESTful API for a booking application. You can request a list of accommodations. And that's where I don't really know how to design the JSON represenation. This is my XML representation:

<?xml version="1.0" encoding="utf-8"?>
<accommodations>
    <accommodation>
        <name>...</name>
        <category>couch</category>
    </accommodation>
    <accommodation>
        <name>...</name>
        <category>room</category>
    </accommodation>
<accommodations>

My first try to convert this to JSON resulted in this output (1):

{
    "0": {
        "name": "...",
        "category": "couch"
    },
    "1": {
        "name": "...",
        "category": "room"
    }
}

But as I looked how others APIs did it, I found something looking more like this (2):

[
    {
        "name": "...",
        "category": "couch" 
    },
    {
        "name": "...",
        "category": "room" 
    }
]

I know version 1 is an object, and version 2 an array.

But which one is better in this case?

A: 

If you are going to use the key (1, 2, ...) as the identifier in communications back to the server the dictionary is better, otherwise I'd go for the array. The unique id then is probably a field in an entry.

extraneon
+1  A: 

You could model the JSON as follows:

{
  "accomodations" : [
    {
      "accomodation" : {
        "name": "...",
        "category": "couch",
        "uri": "http://example.org/accomodations/accomodation_1"
      }
    },
    {
      "accomodation": {
        "name": "...",
        "category": "room",
        "uri": "http://example.org/accomodations/accomodation_2"
      }
    }
  ]
}

and you could return this on a GET http://example.org/accomodations Creating a new accomodation could then be done through a POST http://example.org/accomodations with something like the following in the body:

{
  "accomodation": {
    "name": "...",
    "category": "room"
  }
}
dafmetal
Thanks, I've seen this several times in other APIs. But isn't this the same thing twice? I request "accommodations" (btw: really with 2 m) and get a list - of accommodations of course. Why repeat this? Or is there a technical reason?
Jan P.
Returning an object instead of an array would make it easier for you if you would later need to add fields to it.Say for instance you want to filter on accommodations from a certain hotel through a query parameter, e.g.:GET /accommodations?owner=Hiltonyou might want to include an address of the owner like this:{ "accomodations" : [ /* accomodations here */ ], "owner" : "Hilton", "address" : "Party Street 123",}as you already were returning an object, existing clients don't break. I you were returning an array, existing clients would need to adapted.
dafmetal
Okay, working one day with this style made it clear that it's also easier to nest lists. When you start to, for example, include a list of attributes for a accommodation you have to switch to this approach anyway, so why not use it on the main list, too. It's much more unified this way.
Jan P.