how to write data to the beginning of a file with node / javascript (tutorial and example)

Recently I have had to do a lot of work building infrastructure converting data from .json to .csv. One challenge that presented itself was figuring out a way to prepend / write data to the beginning of the .csv file. There are plenty of use cases for prepending information to the beginning of a file, however in my specific use case I needed to count the number of times a variable existed in the .json data so that I could write out that many columns in the header of the .csv file.

The example below creates a .csv file using data extracted from a .json file via node.js / javascript and then writes the header once the desired counts have been determined. To do so, we must first create a temporary file with the converted data (left), create a new file with the generated header (middle), then copy data from the temporary file into the new file (right).


part 1 – variable setup

We will begin by setting up the variables and functions needed for the task at hand. The header functions have been abstracted and should be written for your specific need(s). Additionally, loading up the entire .json file into memory as depicted in this example has been used for simplicity; implementation of a readstream for reading the .json file would improve performance and allow you to read a larger file.

part 2 – .json to .csv conversion

Once we have the .json data loaded up, we will convert each index in the json array into a comma separated row using our convertIntoCsvString function, add a newline to the end of the row, and write the row to our temporary file csvFilename-tmp.csv using fs.writestream. Additionally, use our getCount function to track how often our deisred event or instance occurs.
Note, some version of async.each should be used to ensure that all the data has been analyzed at and written to the .csv file prior to closing the writestream. If the order of data analysis is significant and there is risk of race conditions to occur, async.eachSeries should be used.

part 3 – header creation

With the desired information in hand, we can now create the custom header using createCsvHeaderUsingCount. The data is read from the temporary file csvFilename-tmp.csv, our header is prepended to this data, and our desired file csvFilename.csv is created. We delete the temporary file using a child process and then are left with our shiny new file ready for consumption.

uncommented code

Below is the code above condensed with comments removed.