Convert JSON to CSV in Scala?

Is there an already available library that can convert a JSON string (most likely more than 1 data string) to a CSV file.

I searched a lot for any such libraries in Scala, but couldn't find anything.

I need to search for data from a database source, the result set is in JSON format and convert it to CSV.

Before I did, I converted JSON to the appropriate Seq [case-class] and tried to use libraries such as:

But this is not very useful if in the case of the case class, which contains deep hierarchies.

Any suggestions

+4
source share
2 answers

product-collections Seq [case class] csv.

case class Foo(a:Int,b:String)
Seq(Foo(1,"aa"),Foo(2,"bb")).csvIterator.mkString("\n")
res27: String =
1,"aa"
2,"bb"

json, , , scala.

" ", , .

0

, . - ?

case class Baz(i: Int)
case class Bar(baz: Baz)
case class Foo(bar: Bar)

kantan.csv : CSV-. , :

import kantan.csv.ops._
import kantan.csv.generic.codecs._

val fs: List[Foo] = ???

fs.foldLeft(new File("output.csv").asCsvWriter[Foo])(_ write _).close

, :

import kantan.csv._

implicit val bazCodec = Codec.caseCodec1(Baz.apply, Baz.unapply)(0)
implicit val barCodec = Codec.caseCodec1(Bar.apply, Bar.unapply)(0)
implicit val fooCodec = Codec.caseCodec1(Foo.apply, Foo.unapply)(0)

, , .

0

All Articles