I am trying to read a json file in order to calculate some metrics in scala. I managed to read the file and to conduct some outer filters but I have troubles understanding how to filter nested lists and maps.
Here is an example code (the real json is longer):
val rawData = """[
{
"technology": "C",
"users": [
{
"rating": 5,
"completed": false,
"user": {
"id": 11111,
"paid": true
}
},
{
"rating": 4,
"completed": false,
"user": {
"id": 22222,
"paid": false
}
}
],
"title": "CS50"
},
{
"technology": "C++",
"users": [
{
"rating": 3,
"completed": true,
"user": {
"id": 33333,
"paid": false
}
},
{
"rating": 5,
"completed": true,
"user": {
"id": 44444,
"paid": false
}
}
],
"title": "Introduction to C++"
},
{
"technology": "Haskell",
"users": [
{
"rating": 5,
"completed": false,
"user": {
"id": 55555,
"paid": false
}
},
{
"rating": null,
"completed": true,
"user": {
"id": 66666,
"paid": false
}
}
],
"title": "Course on Haskell"
}
]"""
val data = rawData.toString.split("\n").toSeq.map(_.trim).filter(_ != "").mkString("")
I managed to get a list containing the 3 titles:
import scala.util.parsing.json._
val parsedData = JSON.parseFull(data)
val listTitles = parsedData.get.asInstanceOf[List[Map[String, Any]]].map( { case e: Map[String, Any] => e("title").toString } )
Here are my 3 questions:
- Is that a good approach to obtain the list of the 3 titles?
- How to obtain a List containing the number of paid users for each of the latter 3 titles?
- How to obtain a List containing the number of users who have completed the course for each of the latter 3 titles?
Thanks in advance for the help