An exercise in converting an array of structs to a query dynamically so you can dump out the data in a compact form
I was working with some JSON data in Javascript and wanted to view the data in a table layout so I could quickly scan it to see what I was working with. This is easily done in a browser using console.table
. For example, using the following Javascript code:
// Javascript data = [ { i: 1, label: "One" }, { i: 2, label: "Two", foo: "Foo" }, { i: 3, label: "Three", bar: "Bar" }, { i: 4, label: "Four", foo: "Foo", bar: "Bar" } ]; console.table(data);
I get a nice compact view of the data in my browser console:
This got me thinking about how you could dump out an array of structs in a compact form using CFML.
Here’s what the standard dump of that array in CFML looks like:
Not too bad to read, but as we get more rows and/or more keys, it’s going to get much harder to visual scan.
I decided that a dump of CFML’s query object is essentially what we are after, so just need to convert the array to a query. As each element in the array has different keys, this needs to be done dynamically. Here’s what I came up with:
// CFML data = [ { i: 1, label: "One" }, { i: 2, label: "Two", foo: "Foo" }, { i: 3, label: "Three", bar: "Bar" }, { i: 4, label: "Four", foo: "Foo", bar: "Bar" } ]; function arrayToQuery(data) { return data.reduce(function(accumulator, element) { element.each(function(key) { if (!accumulator.keyExists(key)) { accumulator.addColumn(key, []); } }); accumulator.addRow(element); return accumulator; }, QueryNew("")); } // writeDump(data); writeDump(arrayToQuery(data));
The output from this looks like:
I’m not intending to use this code in production, it was more of a coding exercise, but thought I’d share.
Runnable code here if you want to hack about with it.
https://cffiddle.org/app/file?filepath=d3656d85-050d-4a86-b26f-9797073991d9/3688841c-1312-47b4-9f85-00880b379a54/7c2c9541-cf21-4ff0-9795-7dcda91b963b.cfm
@Jeff Coughlin Thanks for your comment, I’m aware of the data parameter for QueryNew. I didn’t use that here as you need to know the columns in advance before you can pass in the array. So sure, I could have used arrayReduce to find all the fields names, then used that do the `QueryNew(fields_found_in_array)`, but dynamically adding the cols seemed to be a much neater way to handle it.
This was patched in CF2018 update 5 (allowing you to just send in an array of structs and it essentially does all the same code you wrote in the backend).
queryNew(data);
However, assuming you don’t have CF2018 update 5+ and you don’t have the column names, then you’re out of luck without looping and to get the data like you did. In that case, it’s a cool function (thanks!)
Just for kicks I wrote some code to work with queryNew, an array value, and getting the column names. It worked on the first try, but without realizing it I essentially just rewrote your code (facepalm).
accumulator = “”;
columns = data.reduce(function(accumulator, item) {
item.each(function(key) {
if (!listFind(accumulator, key)) {
accumulator = listAppend(accumulator, key);
}
});
return accumulator;
});
writeDump(queryNew(columnNames = columns, data = data));
This functionality is already built into CF (CF10+):
writeDump(queryNew(“i,label,foo,bar”, “integer,varchar,varchar,varchar”, data));
For more info, see the rowData attribute in queryNew() here: https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-m-r/querynew.html
Yes, very nice. Thanks, John (aka poor yorik. “Alas”, I wonder how many get the quip in your “alias”!)
About the code, I hope you’ll submit it to the cflib site (cflib.org). Folks do still search there for possible CFML solutions to problems.
You’ll see a link there for submitting content, and indeed it’s now hosted on github (so submissions are by PR to that now). Ray is still the project owner, and though there are a couple of PRs outstanding from 2017, it’s worth a shot to see if it would get posted.
Thanks of course for the cffiddle.org link, in the meantime.
You must be logged in to post a comment.