Add a default option in query data transformations to flatten data

I often (50% of the time) flatten my query result as I find them much easier to work with flat. I also find that there dont seem to be many downsides as most retool components treat default and flat daa the same. I use this code in the transform section to flatten, but it would be great if this would be a default toggle, rather than me having this code snippet saved in a note for reuse

// Step 1: Flatten data into an array of objects
const flatData = Array.from(
{ length: Object.values(data)[0]?.length || 0 }, // Determine the number of rows from the first key
(_, index) => {
const record = {};
// Iterate over each key in the data object and map its values to the record
Object.keys(data).forEach(key => {
record[key] = data[key][index];
});
return record;
}
);
return flatData;

I also frequently use flattening. I often add a global function to the preloaded JS in page settings.

Hey @ben_inkana - while not quite as simple as a toggle, we do have a predefined function for exactly this use case. It's pretty trivial to replace return data with return formatDataAsArray(data) in your query's transformer!

1 Like