All posts by admin

Gadget



_..__
.' I '.
|.-"""-.|
_;.-"""-.;_
_.-' _..-.-.._ '-._
';--.-(_o_I_o_)-.--;'
`. | | | | | | .`
`-\| | | |/-'
| | | |
| \_/ |
_.'; ._._. ;'._
_.-'`; | \ - / | ;'-.
.' : / | | | | \ '.
/ : /__ \ \___/ / __\ : `.
/ | / '._/_\_.' \ : `\
/ . `---;"""""'-----` . \
/ | |() () | \
/ /| | |\ \
/ / | |() () | \ \
| |
\ \ |][ | | ][ | / /
\ \ ;=""====='"""'====""==; / /
|/`\ \/ |() () \/ /`\|
|_/.-'; | |`-.\_|
/ | ; : \
|__.| | |.__|
; | |
| : ;
| : |
; | |
; | ;
| : |
| | ;
| | ;
'-._ ; _.-'
`;"--.....--";`
| | | |
| | | |
| | | |
T----T T----T
_..._L____J L____J _..._
.` "-. `% | | %` .-" `.
/ \ .: :. / \
'-..___|_..=:` `-:=.._|___..-'

Project name

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
  }

}