Complex Table Data
Recently my team and I have been looking at ways to represent really large object graphs in such a way that the consumers of our services can render parts of the data as data tables. After all, a big part of what our company does is big data analytics — so it only makes sense that we’d report some of this calculated results set as a table of data.
Here’s the fun part, though. We can’t always assume that our consumer is going to represent and render the data the same way as it’s being represented and rendered right this second. Yes, we’re well aware that our current (and only) client platform is a SPA written in AngularJS, so we need the data to be easily rendered in some kind of formatted HTML.
But, we’re also well aware that we’re going to be breaking into mobile computing at some point. And the way we represent the same data on a tablet or smartphone will have to look dramatically different than the way we represent it on a computer screen in a browser.
At a fundamental level, key/value naming is a big part of the problem again. Theoretically, you should be able to just convert the deep object graph into key value pairs to render on the clients consuming the data. The reality is, sometimes the rendering makes the problem more complex than that and causes us to have massive confusion about how the whole thing should work.
Maybe it’s just a matter of making sure that the objects you’re serializing are properly nesting other objects and collections beneath them. I haven’t actually taken the time yet to look at our object graph to decide if it makes more sense to build up some kind of translation object (or DTO) to represent the data we’re after.
Right now we’re just turning our data into a key/value representation to send as a response, without actually applying any actual object state into the serializer.
Get Jason Lowenthal’s stories in your inbox
Join Medium for free to get updates from this writer.
Regardless, whether you’re just rendering a key/value map directly or creating some kind of container object for the response. Please, please…. PLEASE… do not do this:
{
"table": [
{
"header_row": [
{
"header_column": {}
}
],
"row": [
{
"column": {}
}
]
}
]
}We’re not rendering HTML and telling the consumers of our data how to make a table. Even if you are absolutely certain that the team designing the UX is going to plug this into a table representation on an HTML page — it’s not really a good idea to assume that the first consumer of the data will be the only consumer of your data.
If we had originally started our APIs to follow the pattern of http://jsonapi.org/ then this discussion wouldn’t have even really had to happen I don’t think. It clearly outlines the way top level objects and collections within those objects are supposed to be represented in a JSON API heirarchy. I’m not going to spend a significant amount of depth going into how we could have, would have, should have. Instead I’m going to look at what our JSON ended up looking like (more or less). It’s hardly following any kind of JSON API standard, but it’s definitely way better than before. Like you see above. <Cough>.
{
"section_name": "Awesome Report Section",
"top_level_section_specific_data": [
{
"top_level_object_id": 1,
"second_level_section_specific_data": [
{
"second_level_object_id": 1,
"second_level_object_name": "Thing 1",
"third_level_section_specific_data": [
{
"section_title_text": "Knock Your Socks Off!",
"count": 200
},
{
"title_text": "More Socks Are Knocking!",
"count": 190,
"percent": 95
},
{
"title_text": "Look! They're Sockless",
"count": 180,
"percent": 90
},
{
"title_text": "Seriously, No Socks here!",
"count": 160,
"percent": 80
}
]
},
{
"second_level_object_id": 2,
"second_level_object_name": "Thing2",
"third_level_section_specific_data": [
{
"section_title_text": "Knock Your Socks Off!",
"count": 200
},
{
"title_text": "More Socks Are Knocking!",
"count": 190,
"percent": 95
},
{
"title_text": "Look! They're mSockless",
"count": 180,
"percent": 90
},
{
"title_text": "Seriously, No Socks here!",
"count": 160,
"percent": 80
}
]
}
]
},
{
"top_level_object_id": 2,
"second_level_section_specific_data": [
{
"second_level_object_id": 1,
"second_level_object_name": "Thing 1",
"third_level_section_specific_data": [
{
"section_title_text": "Knock Your Socks Off!",
"count": 200
},
{
"title_text": "More Socks Are Knocking!",
"count": 190,
"percent": 95
},
{
"title_text": "Look! They're Sockless",
"count": 180,
"percent": 90
},
{
"title_text": "Seriously, No Socks here!",
"count": 160,
"percent": 80
}
]
},
{
"second_level_object_id": 2,
"second_level_object_name": "Thing2",
"third_level_section_specific_data": [
{
"section_title_text": "Knock Your Socks Off!",
"count": 200
},
{
"title_text": "More Socks Are Knocking!",
"count": 190,
"percent": 95
},
{
"title_text": "Look! They're Sockless",
"count": 180,
"percent": 90
},
{
"title_text": "Seriously, No Socks here!",
"count": 160,
"percent": 80
}
]
}
]
}
]
}The team and I probably spent a good hour figuring out the naming scheme we wanted to follow to make sure that our nested JSON told an accurate story about the data it’s representing. While it’s not really in a spectacular JSON API format, it’s going to be much more straightforward to refactor this down the road than its initial starting point. One of our properties was even called “columns”. <Shudder>
As you’re working on data structures for APIs, make sure that the structure itself isn’t built to present data for the purpose of a specific end result. It’s very easy to get caught in the trap of writing API responses that are explicitly catering to the UX that they’re communicating with. After all, it’s the UX that informs the design of the API in the first place.
Trying to make API responses fit into specific UX paradigms makes them extremely inflexible, and really reduces or eliminates their re-usability later. Admittedly, the data that I’ve presented here is part of a much larger JSON structure, and I can’t imagine any other part of the application needing to call this besides the very specific use case that it’s originally being designed for.
But, at least now we’ve positioned ourselves so that we can start breaking down the data into smaller chunks. Making it easier, later, to convert this into a reusable API pattern.
Originally published on my defunct Wordpress site on February 11, 2016.








