I took a moment yesterday morning to start work on a Ruby script to convert CSV files to JSON. My main motivation for writing the script was to get data into a format that more easily works with D3.js.
The script builds JSON using the first line of the CSV file to determine the appropriate elements and the corresponding data, as well as checking that integers and strings are converted to their appropriate (typed) formats. And the script seems fairly robust; I noticed hardly a pause when throwing a 14,000-line CSV file at it. There are no built-in specifics to the data mapping; you’ll need to handle that either by modifying the script or handling it in D3.
Say I give it an input like this (postwar population of Bay Area cities):
And in return I get this:
Note that the data transformation for this particular example probably isn’t the best. A more useful JSON construction would group the data together with child nodes, for example:
But if you need a simple CSV to JSON transformation, the script starts getting you there.