score:-1

Try to parse from the file with jsoniter-scala, it will clearly report a position and a cause of the problem.

https://github.com/plokhotnyuk/jsoniter-scala

score:0

Is """ mandadory for JSON records in your text file? I removed them and it works for me.

Results I've got in console:

Name from file "firstName" 
Name from file "secondName"

Source code:

import org.json4s.jackson.JsonMethods.{parse, pretty}
import scala.io.Source

object Json4sTest {
  def map_fields(lines: String) = {
    val parseJSON = parse(lines)
    println("Name from file " + pretty(parseJSON \ "Information" \ "Assets" \ "Asset" \ "Name"))
  }
  def main(args: Array[String]): Unit = {
    val file = Source.fromFile("testFile.txt").getLines()
    file.foreach(map_fields)
  }
}

testFile.txt:

{"Information":{"Context":"firstContext", "Assets":{"Asset":{"Name":"firstName"}}}}
{"Information":{"Context":"secondContext", "Assets":{"Asset":{"Name":"secondName"}}}}

score:0

The reason why it fails is because the lines in the file are not valid JSON strings. A JSON string cannot start with triple quotes or quotes for that matter, unless it's just a simple string.

Triple quote(""") in scala is used to create multi-line strings and strings that contain quotes within them. You need to use them only when you define string literals(hard code strings) in scala.

So, remove the triple quotes from the lines in the file and it should give you proper results.


Related Query

More Query from same tag