Renaming a CSV column header name in Retool

Hi everyone - We often get asked about renaming CSV column names in Retool. One of the easiest ways to do so is through a JS query that returns the modified CSV. You can see the example below:


// We have a CSV that contains the following data:
// Year,Make,Model
// 1997,Ford,E350
// 2000,Mercury,Cougar

// 1. We parse the CSV and format it as an object:
let csv = formatDataAsObject(filepicker1.parsedValue)

// The data object contains an array with three json objects for each column:
// {Year: Array(2), Make: Array(2), Model: Array(2)}
// Make: (2) ["Ford", "Mercury"]
// Model: (2) ["E350", "Cougar"]
// Year: (2) ["1997", "2000"]

// 2. We copy contents of 'Make' into a new key called 'Brand'
csv.Brand = csv.Make

// Now the data object also contains a key called 'Brand'
// that contains the same data as 'Make'
// Brand: (2) ["Ford", "Mercury"]
// Make: (2) ["Ford", "Mercury"]
// Model: (2) ["E350", "Cougar"]
// Year: (2) ["1997", "2000"]

// 3. Delete the old key.
delete csv.Make

// Now, data contains the following array:
// {Year: Array(2), Brand: Array(2), Model: Array(2)}
// Brand: (2) ["Ford", "Mercury"]
// Model: (2) ["E350", "Cougar"]
// Year: (2) ["1997", "2000"]

return csv 
// or return formatDataAsArray(csv)

In this specific example the CSV is uploaded via the filepicker1 component.

5 Likes