How should I represent tabular data in JSON?

Synthesizing other answers:

  1. Your wire format doesn't have to be the same as your in-memory format.
  2. Profile which is better - see if it makes a difference.
  3. Simpler is usually better to start with.

Further:

  • If you just have a page of results, and few users, then the 2nd format may be no worse than the 1st format.
  • If your data is quite sparse, the 2nd format may well be better.
  • If you're sending 1000's or rows of data, and you have millions of users, then it's possible that the size of data you send can start to matter, and perhaps the 1st format may help.
  • You can't guarantee that all user agents support gzip / deflate, so bear this in mind.

Profile both. Optimize afterwards.


Just another JSON structure from which I got very nice results:

{
    "recordCount": 2,
    "data": {
        "Id": [1, 2],
        "Title": ["First record", "Second record"],
        "Value": [18192, 18176]
    }
}

Traversing all data:

for (var i = 0; i < recordSet.recordCount; ++i) {
    console.log("Record " + i.toString() + ":");
    for (var field in recordSet.data)
        console.log("\t" + field + ": " + recordSet.data[field][i].toString());
}

You don't have to tie your code to the more compact, but also more cumbersome format. Just write a simple JS adapter to check the returned structure for the presence of columns. If that's missing you're dealing with a plain array of objects. If it's present you can easily map the cumbersome format to the more convenient format.