Skip to content

Instantly share code, notes, and snippets.

@dylanmei
Created March 25, 2014 13:50
Show Gist options
  • Select an option

  • Save dylanmei/9762211 to your computer and use it in GitHub Desktop.

Select an option

Save dylanmei/9762211 to your computer and use it in GitHub Desktop.

Revisions

  1. dylanmei created this gist Mar 25, 2014.
    21 changes: 21 additions & 0 deletions csv-to-json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    import groovy.json.*

    if (args.size() == 0) {
    println("missing argument")
    System.exit(1)
    }

    def f = new File(args[0])
    if (!f.exists()) {
    println("file doesn't exist")
    System.exit(1)
    }

    def lines = f.readLines()
    def keys = lines[0].split(',')
    def rows = lines[1..-1].collect { line ->
    def i = 0, vals = line.split(',')
    keys.inject([:]) { map, key -> map << ["$key": vals[i++]] }
    }

    println(JsonOutput.toJson(rows))