Forum Discussion
You should be able to do this through the expression language using the .extend()
method to construct objects and the .map()
method to reshape the elements of the arrays.
Here’s a pipeline with your input data to demonstrate:
FlattenJson_2018_12_26.slp (11.6 KB)
In that pipeline, the first mapper converts the column array to an object indexed by the column IDs to make it easier to do that lookup. The expression in that mapper looks like the following:
{}.extend($myCols.map(col => [col.id, col]))
The .map()
method converts the array into an array of key/value pairs that is then passed to the .extend()
method which will construct the object. Now, we can lookup the title
for a cell by doing $myCols[cell.columnId]
.
With the columns in an easier form, the next mapper iterates over the list of rows to convert the cells into objects with this expression:
$myRows.map(row => { rowNumber: row.rowNumber }.extend(
row.cells.map(cell => [$myCols[cell.columnId].title, cell.get('value')]))
)
The idea here is basically the same, the .map()
call for the cells produces a key/value pair that is passed to .extend()
to produce the object. The only real difference here is instead of calling .extend()
on an empty object, we initialize it with the rowNumber
property.
(I’ve filed a feature request to add some functions to try to simplify this pattern a bit, arriving at these expressions is a bit round-a-bout…)
Nice!
Both solutions worked very well and gave me some great examples of using the script snap and the .extend method.
Thanks to you both and happy new year!