shivani thakur
shivani thakur

Reputation: 229

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?

Upvotes: 1

Views: 1111

Answers (2)

shivani thakur
shivani thakur

Reputation: 229

I added the following line to get the desired output:

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

Upvotes: 1

Learner
Learner

Reputation: 1190

You can use testRegJs \\ "id" for getting the id.

Upvotes: 0

Related Questions