Reputation: 7067
I am using play framework and Scala.
I have following list-
List(
(C: - read,1412750402124,46552070),
(C: - write,1412750402124,433057),
(E: - read,1412750402124,5435),
(E: - write,1412750402124,1728),
(F: - read,1412750402124,5435),
(F: - write,1412750402124,1728),
(C: - read,1412750402125,46552071),
(C: - write,1412750402125,433060),
(E: - read,1412750402125,5445),
(E: - write,1412750402125,1730),
(F: - read,1412750402125,5450),
(F: - write,1412750402125,1428)
)
I want following output-
key:[
{"name":"C:-read",
data:[[1412750402124,46552070],[1412750402125,46552071]...]
},
{
"name":"C:-write",
data:[[1412750402124,433057],[1412750402125,433060]...]
},
{
"name":"E:-read",
data:[[1412750402124,5435],[1412750402125,5445]...]
},
{
"name":"E:-write",
data:[[1412750402124,1728],[1412750402124,1730]...]
},
{
"name":"F:-read",
data:[[1412750402124,5435],[1412750402125,5450]...]
},
{
"name":"F:-write",
data:[[1412750402124,1728],[1412750402124,1428]...]
}
]
How do I get above output using scala??
Upvotes: 0
Views: 622
Reputation: 1883
The input List cannot be compile and some info is missing so I make some assumptions.
val lst = List(
("C: - read", 1412750402124L, 46552070),
("C: - write", 1412750402124L, 433057),
("E: - read", 1412750402124L, 5435),
("E: - write", 1412750402124L, 1728),
("F: - read", 1412750402124L, 5435),
("F: - write", 1412750402124L, 1728),
("C: - read", 1412750402125L, 46552071),
("C: - write", 1412750402125L, 433060),
("E: - read", 1412750402125L, 5445),
("E: - write", 1412750402125L, 1730),
("F: - read", 1412750402125L, 5450),
("F: - write", 1412750402125L, 1428))
Since the results is grouped by the first String (eg "C: - read") the first step is to do the grouping:
scala> val groupedData= lst.groupBy(_._1).map{ case (k,v) => k -> v.map(i => List(i._2, i._3))}.toList
groupedData: List[(String, List[List[Long]])] = List((C: - write,List(List(1412750402124, 433057), List(1412750402125, 433060))), (E: - read,List(List(1412750402124, 5435), List(1412750402125, 5445))), (F: - write,List(List(1412750402124
, 1728), List(1412750402125, 1428))), (F: - read,List(List(1412750402124, 5435), List(1412750402125, 5450))), (E: - write,List(List(1412750402124, 1728), List(1412750402125, 1730))), (C: - read,List(List(1412750402124, 46552070), List( 1412750402125, 46552071))))
Note that the results is List[(String, List[List[Long]])]
. This will make the serialization easier.
Play has great Json library and it can easily be done with 'Writes':
implicit val itemWrites = new Writes[(String, List[List[Long]])] {
def writes(t: (String, List[List[Long]])) = Json.obj(
"name" -> t._1,
"data" -> t._2)
}
and now it can be serialize:
scala> Json.toJson(groupedData)
res23: play.api.libs.json.JsValue = [{"name":"C: - write","data":[[1412750402124,433057],[1412750402125,433060]]},{"name":"E: - read","data":[[1412750402124,5435],[1412750402125,5445]]},{"name":"F: - write","data":[[1412750402124,1728]
,[1412750402125,1428]]},{"name":"F: - read","data":[[1412750402124,5435],[1412750402125,5450]]},{"name":"E: - write","data":[[1412750402124,1728],[1412750402125,1730]]},{"name":"C: - read","data":[[1412750402124,46552070],[141275040212 5,46552071]]}]
Upvotes: 1