06-07-2018 02:24 PM
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.
06-08-2018 03:38 PM
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.
06-12-2018 01:19 PM
Thanks tstack. That’s what we ended up implementing. Agreed, it is not great, especially for a document with dozens of header columns.
09-28-2020 11:33 AM
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.
01-08-2021 02:17 PM
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