Skip to content

Instantly share code, notes, and snippets.

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 alpoza/26f4177d85d3d134c350b9752bcf772a to your computer and use it in GitHub Desktop.
Save alpoza/26f4177d85d3d134c350b9752bcf772a to your computer and use it in GitHub Desktop.
Groovy script to convert Csv to Json
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
*/
/**
* 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
*
*/
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
}
// the maximum number of rows that will be converted, if 0 then all rows will be converted
Integer maxNumberOfRows = new Integer(options.r ?: 0)
// the separator used for splitting the csv columns
String separator = (options.s ?: ",")
def dataList = []
def lineCounter = 0
def headers = []
new File(options.i).eachLine() { line ->
// skip the header; header is line 1
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 = lineCounter + 1
}
String output = JsonOutput.toJson(dataList)
if(!options.o) {
print output
} else {
new File(options.o).write(output)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment