I'm writing an API for retrieving data from a JDBC-connected Java Servlet via JSON. I've chosen to use JSON because we'll want to do sorts and other operations on the data in the browser, and we'll be accessing the data from across domains.
Since I'm essentially doing SQL queries in JavaScript, the data that comes back is tabular in nature. I started to write this so that you get back a list of column labels, then arrays of values, for example:
{ "columns": [ "given_name", "surname", ], "results": [ [ "Joe", "Schmoe" ], [ "Jane", "Doe" ] ] }
But as I start to write the JavaScript to deal with the returned data, I wonder if it might be better to just output the results with key/value pairs, such as:
{ "results": [ { "given_name": "Joe", "surname": "Schmoe" }, { "given_name": "Jane", "surname" : "Doe" } ] }
If you're returning a lot of results, that's a lot of repeated text. But we're going to be transporting gzipped, so I'm not too concerned about bandwidth.
Basically, should I engineer this so that I'm accessing my data with
$.getJSON(query, function(data) { var columns = data.columns; var results = data.results; $.each(results, function(key, row) { console.log(row[columns.indexOf('surname')]); }); });
or the much prettier
$.getJSON(query, function(data) { var results = data.results; $.each(results, function(key, row) { console.log(row.surname); }); });
?
Essentially, I want to know if the potential hit to performance justifies the much cleaner syntax of the latter option.
I did implement it both ways and profile. Profiling was a great idea! The differences in performance were marginal. The differences in data transfer size were substantial, but with Gzip compression, the variance was down to 5-6% between both formats and between very large and very small data sets. So I'm going with the prettier implementation. For this particular application, I can expect all clients to support Gzip/Deflate, so the size doesn't matter, and the computational complexity on both the client and server is similar enough that it doesn't matter.
For anyone interested, here is my data with graphs!.
A Tabular Data Resource MUST be a Data Resource , that is it MUST conform to the Data Resource specification . The data the Data Resource describes MUST: If non-inline: Be a CSV file. If inline data: be “JSON tabular data” that is array of data rows where each row is an array or object (see below)
The main purpose of JSON_TABLE is to create a row of relational data for each object inside a JSON array and output JSON values from within that object as individual SQL column values. You can specify JSON_TABLE only in the FROM clause of a SELECT statement.
Some tips working with JSON: Whitespace (Space, Horizontal tab, Line feed or New line or Carriage return) does not matter in JSON. It can also be minified with no affect to the data.
Profile both. Optimize afterwards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With