cancel
Showing results for 
Search instead for 
Did you mean: 

Empty CSV File With Header Row

Schevus
New Contributor III

I have a need to write an “empty” CSV file that includes only the header row information in the case that there’s no data to populate the file. Does anyone know a good way to do this? Anything to do with the CSV formatter and the header row options doesn’t seem to be working out. It’s always writing a row of data with blanks for each element separated by the delimiter. Appreciate any thoughts, thanks.

7 REPLIES 7

tstack
Former Employee

I don’t think it’s possible to configure the CSV Formatter to do this at the moment. You might need to do something like the following to detect when there is no data:

So, on the ‘No Data’ branch, you can have a Mapper that defines the headers and a CSVFormatter that writes the document to the file. Not great, I wish I had a better answer.

Schevus
New Contributor III

Thanks tstack. That’s what we ended up implementing. Agreed, it is not great, especially for a document with dozens of header columns.

ronmwhite
New Contributor II

For others whom happen to search for this as I just did. The router approach is still required as far as I can tell for the “No Data” condition, but, an easier way to write a header only file is to parse your JSON and use a Constant with a File Writer. For example, the following will get a list of Keys from your JSON (headers) as an array, turn it into a string, then, remove the opening and closing brackets.

JSON.stringify(Object.keys($)).replace(“[”,“”).replace(“]”,“”)

You can drop that into a Constant Snap and write to file. Works well for this case.

mtran21
New Contributor III

Can you share a screenshot of your pipeline that handle this? from the router.
I am in the same situation. I need to write an empty with header CSV file if there is no input data.
Thanks