2

For list API, such as Users, I find that there are two common formats. Arrays:

// flat array
[
  {
    "login": "octocat",
    "id": 1
    ...
  },
  ...
]

GitHub, Heroku and Twitter use this format

VS Objects:

{
  "results": [
    {
      "login": "octocat",
      "id": 1
      ...
    },
    ...
  ]
}

Parse and Slack and instagram use this format.

Which one is better? How to compare these two formats?

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
Tony Han
  • 2,130
  • 3
  • 24
  • 37
  • 3
    I would argue that the second method is more extensible. It is backwards compatible in the event that you'd like to, for example, include some metadata in the object under another member name. The first method would be guaranteed require a breaking change in the event that you wanted to add to the schema. – CollinD Oct 03 '15 at 08:19
  • Make sense. So I just don't understand why GitHub, Heroku and Twitter use the first method. – Tony Han Oct 03 '15 at 12:19

1 Answers1

1

Array-oriented data has the following advantages:

  • Client-side JavaScript has more methods for arrays than objects
  • No need for root node to simulate a tree structure for a tabular structure

and disadvantages:

  • No namespacing since indexes are numeric
  • JSON.parse fails in client-side JavaScript

Key/Value-oriented data has the following advantages:

  • JSON.parse works in client-side JavaScript
  • Namespacing makes duplicate values easier to disambiguate
  • JSON.stringify works to make simple transformations easier compared to array methods

and disadvantages:

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265