1

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.