Search code examples
jsonscalaseq

Iterate through JSON in Scala


sorry if it's a basic question. When I'm running the following code to print testRegJs:

val testRegJs: Seq[JsValue] = for (tr <- testReg) yield Json.toJson(tr)

Note: here testReg is list of certain criteria i.e sequence of object and in the above code it is converted to seq[JsValue].

Output:

List({
"registration": {
    "id": 495,
    "profile_id": "755"
},
"test_center": [{
    "id": 487,
    "registration_id": 495
}]
}, {
"registration": {
    "id": 599,
    "profile_id": "360"
},
"test_center": [{
    "id": 594,
    "registration_id": 599
}]
})

I want to print id from the above list. How it can be done?


Solution

  • I added the following line to get the desired output:

    val regId = testRegJs.map(x => (x \ "registration" \ "id").as[Int])