-1

Sorry if the question is worded badly, I couldn't think of a short way of explaining it without examples. My question is if you are hosting a JSON API, which is better:

[
  {
    "name": "Foo",
    "age": 1
  },
  {
    "name": "Bar",
    "age": 2
  }
]

or

{
  "Baz": [
    {
      "name": "Foo",
      "age": 1
    },
    {
      "name": "Bar",
      "age": 2
    }
  ]
}

My personal opinion is the 2nd one, however, I'm not sure if it's unnecessary since no other keys will be added, at least not yet, but I guess it gives flexibility so you don't make any breaking changes.

0
{
  "Baz": [
    {
      "name": "Foo",
      "age": 1
    },
    {
      "name": "Bar",
      "age": 2
    }
  ]
}

This is not a dictionary. It is an object with a list property. Those are two very different things.


As to the question of which one you need to use, that is highly dependent on the method and the data you intend to send.

If the method is e.g GetCompanies, then it makes sense to return a list of companies. You don't need a wrapping object.

If you intend to add some metadata to your return values (e.g. if this is paginated data, you may want to send a "total count"), then you will want to return an object which contains both the metadata and the data (list).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.