Skip to content

Instantly share code, notes, and snippets.

@mpas
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpas/d00b3b29235b2a49e810 to your computer and use it in GitHub Desktop.
Save mpas/d00b3b29235b2a49e810 to your computer and use it in GitHub Desktop.
Simple Csv to JSON converter in Groovy
import groovy.json.JsonOutput
/**
* A simple CSV file to Json converter
*
* The CSV file is expected to have a header row to identify the columns. These
* columns will be used to generate the corresponding Json field.
*
* @author Marco Pas
*/
class CsvToJsonConverter {
// the maximum number of rows that will be converted, if 0 then all rows will be converted
private Integer maxNumberOfRows = 0
// the file used as input for the conversion
private File inputFile = null
// place holder for the converted data
private String output = ""
// the separator used for splitting the csv columns
private String separator = ","
String getSeparator() {
return separator
}
void setSeparator(String separator) {
this.separator = separator
}
CsvToJsonConverter(File input, Integer numberOfRows) {
inputFile = input
maxNumberOfRows = numberOfRows
}
CsvToJsonConverter(File input, Integer numberOfRows, String separator) {
this(input, numberOfRows)
this.separator = separator
}
private CsvToJsonConverter convert() {
def headers = []
def dataList = []
def lineCounter = 0
inputFile.eachLine() { line ->
if(maxNumberOfRows == 0 || lineCounter <= maxNumberOfRows) {
if(lineCounter == 0) {
headers = line.split(separator).collect{it.trim()}.collect{it.toLowerCase()}
} else {
def dataItem = [:]
def row = line.split(separator).collect{it.trim()}.collect{it.toLowerCase()}
headers.eachWithIndex() { header, index ->
dataItem.put("${header}", "${row[index]}")
}
dataList.add(dataItem)
}
}
lineCounter += 1
}
output = JsonOutput.toJson(dataList)
return this
}
public void toFile(File fileOutput) {
convert()
fileOutput.write(output)
}
public String toString() {
convert()
return output
}
/**
* Example usage:
*
* groovy CsvToJsonConverter.groovy -i /data/inputfile.csv
* groovy CsvToJsonConverter.groovy -i /data/inputfile.csv -r 100
* groovy CsvToJsonConverter.groovy -i /data/inputfile.csv -o /data/outputfile.json
*
*/
public static void main(String[] args) {
def cli = new CliBuilder(usage: 'CsvToJsonConverter.groovy -[hiors]')
cli.with {
h longOpt: 'help', 'Show usage information'
i argName: 'input', args: 1, longOpt: 'input-file', 'The filename used for input', required: true
o argName: 'output', args: 1, longOpt: 'output-file', '(optional) The filename of the output file, if no filename is given output will be shown on console'
r argName: 'number', args: 1, longOpt: 'rows', '(optional) The number of rows that will be process (default 0 = all rows)'
s argName: 'separator', args: 1, longOpt: 'separator', '(optional) The separator used when converting the Csv file (default ,)'
}
String errorMsg = "Invalid arguments.\nusage: ${cli.usage}\n" +
"Try `CsvToJsonConverter --help' for more information."
def options = cli.parse(args)
if (!options) {
return
}
if (options.h) {
cli.usage()
return
}
if(!options.i) {
println errorMsg
return
}
if(!options.o) {
print new CsvToJsonConverter(new File("${options.i}"), new Integer(options.r ?: 0), (options.s ?: ","))
} else {
new CsvToJsonConverter(new File("${options.i}"), new Integer(options.r ?: 0), (options.s ?: ",")).toFile(new File("${options.o}"))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment