Reputation: 3325
Suppose I have a file:
JsonsFile.json
{"key1":"value11","key2":"value12","key3":"value13"}
{"key1":"value11","key2":"value12","key3":"value13"}
{"key1":"value11","key2":"value12","key3":"value13"}
It may have variable number of Jsons. How can I get List of Maps from this file? I would like to access elements like list(i)("key2")
Upvotes: 1
Views: 49
Reputation: 1311
There are a number of scala libraries that process json, but I'm partial to json4s. this can easily parse json into scala, but the direct result isn't a map. If your json records posses a regular format (as your example suggests) then I would recommend something like this
import org.json4s._
import org.json4s.jackson.JsonMethods._
import scala.io.Source
case class Record(key1:String, key2:String, key3:String)
implicit val format = DefaultFormats
val records = Source.fromFile("JsonFile.json").getLines.map(parse(_).extract[Record]).toList
\\ records will be a List[Record], with elements accessible like
records(1).key2
Upvotes: 1