Json in Scala

https://www.playframework.com/documentation/2.6.x/ScalaJson

name := "scala-json-sbt"

version := "0.1"

scalaVersion := "2.12.6"

libraryDependencies += "com.typesafe.play" %% "play-json" % "2.6.0"
import java.io.File

import play.api.libs.json.{JsValue, Json}

object JsonToCsv extends App {

  val JsonDirectory: String = "src/main/resources/json/"

  val files = getFileTree(new File(JsonDirectory)).filter(_.getName.endsWith(".json"))

  for (file <- files) {
    println(s"${"*"*80}\nprocessing ${file}\n${"*"*80}\n")
    val lines: String = getLinesFromFile(file)


    val parsedData: JsValue= Json.parse(lines)
    println(parsedData)

    val children = parsedData \\ "fields"
    println (children)
    println (parsedData \ "fields")
    
    children.foreach(c =>
      println(c \\ "attributes")
    )

  }

  def getFileTree(f: File): Stream[File] =
    f #:: (if (f.isDirectory) f.listFiles().toStream.flatMap(getFileTree)
    else Stream.empty)

  private def getLinesFromFile(file: File) = {
    val source = scala.io.Source.fromFile(file)
    val lines = try source.mkString finally source.close()
    lines
  }

}

Leave a Reply

Your email address will not be published. Required fields are marked *