register
other register

Wednesday, June 20, 2012

Validate json files in sub directories

import scala.io.Source._
import scala.util.parsing.json.JSON
import java.io._

object JsonValidator {
  def main(args: Array[String]) {

    for (file <- recursiveListFiles(new File(".")).filter(_.getName.endsWith(".json"))) {
      val json = JSON.parseFull(fromFile(file).getLines mkString)
      if (json.isEmpty)  // if json file is not valid json
        println(file + ": " + json)
    } 
  }

  def recursiveListFiles(f: File): Array[File] = {
    val these = f.listFiles
    these ++ these.filter(_.isDirectory).flatMap(recursiveListFiles)
  }
}

No comments: