Skip to content

Instantly share code, notes, and snippets.

@mpas
Last active November 2, 2023 06:58
Show Gist options
  • Save mpas/58497115057068f15751 to your computer and use it in GitHub Desktop.
Save mpas/58497115057068f15751 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