Skip to content
Transforms JSON collections into CSV files.
Branch: master
Clone or download
agourlay Merge pull request #16 from scala-steward/update/sbt-coursier-2.0.0-R…
…C2-1

Update sbt-coursier to 2.0.0-RC2-1
Latest commit 57a0eff Jul 3, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
project Update sbt-coursier to 2.0.0-RC2-1 Jul 2, 2019
src scala 2-13 Jun 13, 2019
.gitignore scala 2-13 Jun 13, 2019
.travis.yml scala 2-13 Jun 13, 2019
LICENSE.txt Scala 2.11.5 Jan 8, 2015
Readme.md 2.13 badge Jun 13, 2019
build.sbt scala 2-13 Jun 13, 2019
version.sbt Setting version to 0.5.1-SNAPSHOT Jun 13, 2019

Readme.md

json-2-csv Build Status Maven Central Join the chat at https://gitter.im/agourlay/json-2-csv

A library transforming JSON collections into CSV files.

Features

  • ingests JSON collections from a File or from a Stream[String].
  • nested JSON objects are turned into extra CSV columns and lines.
  • works in a streaming fashion with a small memory footprint.

Limitations

  • the JSON objects in the collection level must share a common structure.
  • the first element should be a complete definition of the structure, the following elements can be sparse.
  • the transformation stops at the first error encountered.

Input & output formats

A file containing a JSON collection like this is transformed into a CSV file like that.

When nested objects are turned into extra columns the content of the parent object is not repeated.

APIs

Two methods on the Json2Csv object returning an Either of the number of CSV lines written to the OutputStream:

def convert(file: File, resultOutputStream: OutputStream): Either[Exception, Long]

def convert(chunks:  LazyList[String], resultOutputStream: OutputStream): Either[Exception, Long]

Usage example

object Boot {
  def main(args: Array[String]): Unit = {
    if (args.isEmpty) println("Error - Provide the CSV file path as argument ")
    else {
      val output = new FileOutputStream("result-json.csv")
      Json2Csv.convert(new File(args(0)), output) match {
        case Right(nb) => println(s"$nb CSV lines written to 'result-json.csv'")
        case Left(e)  => println(s"Something bad happened $e")
      }
    }
  }
}

Installation

libraryDependencies ++= Seq(
  "com.github.agourlay" %% "json-2-csv" % "0.5.0",
  ...
)

License

This code is open source software licensed under the MIT License.

You can’t perform that action at this time.