Should data be formatted in the backend or front-end?

Great question. I do a layered MVC-inspired architecture.

BACKEND

I model (format) the data on the backend in accordance with it's 'natural' order. In other words I follow the internal organization of the data. This is because my APIs are often used by multiple, changing or evolving clients and rewriting the API multiple times or having multiple versions takes too much time to maintain.

This doesn't mean you should send the contents of the database with every API call. You should definitely pare down the data model with for each call but it should be a pared down version of the backend ('natural') data model not a data structure that is customized to a particular view.

FRONTEND

In the front end I have a tightly coupled controller which receives the data from the server and transforms the data into the model that fits well with a given view. Depending on the technology used on the client side there may be library support for this (e.g. AngularJS for javascript/HTML Swing for Java, WPF for C#, etc. etc.)

I find that this architecture results in clean separation and high productivity.


It really depends on the nature of transformation you need to do with your data and also on how frequently a certain type transformation is needed.

I'd make the backend return the raw data by default, but for particular data formats that are often required by the frontend, I'd make the backend endpoint accept a request parameter that tells the backend in what format it should return the data.