Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Put your API on a JSON diet

Paul Venezia | March 31, 2015
Last week I discussed design considerations for APIs, given that APIs aren't applications and shouldn't be treated as such. At small scales, APIs that come along for the ride with bulky Web frameworks might be fine, but beyond that you're asking for trouble. If you're building an API that will serve a large number of clients, your API code should be thin and tight, as well as make liberal use of caching. Otherwise, the future headaches will be crippling.

Here we're talking only about transmission considerations. Regardless of whether the data is stored at the API side, the bandwidth used to deliver that data matters — on both the client and server side. In an age where mobile users and even some unfortunate wired broadband users have data caps, every little bit of savings can go a long way.

Of course, it might be tempting to get carried away and render your JSON as functional nonsense, abbreviating like so:

{    "st" : [{        "cds" : { "la": "nn.nnnnn", "ln": "-nn.nnnn" },        "dur" : 500    }, {        "cds" : { "la": "nn.nnnnn", "ln": "-nn.nnnn" },        "dur" : 500    }, {        "cds" : { "la": "nn.nnnnn", "ln": "-nn.nnnn" },        "dur" : 500    }]}

This saves a further 33 bytes per snippet, but at the significant expense of readability, especially if you're mapping database column names to their JSON equivalents. It pays to be judicious about where you apply a slimming strategy because the second example above is as readable as the first, but still saves on bandwidth. The third is mostly unintelligible unless you know exactly what it represents. That knowledge comes at another price.

A decent comparison could be made to Unix in this regard. One can only imagine the number of keystrokes and bandwidth saved over the decades because the copy command is cp not copy and move is mv not move. Being concise is not bad, but it can go too far.

There's a fine line between slimming down your JSON and turning your API into alphabet soup, but if you are expecting to service anything at a significant scale, definitely look at where you can put your API on a diet. As with so very many facets of IT and life, moderation is key.

 

Previous Page  1  2 

Sign up for CIO Asia eNewsletters.