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


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}

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()


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


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