Aakanksha Choudhary
Aakanksha Choudhary

Reputation: 625

Parse JSON Array in Scala

I have this jsArray (json Array) and I am using import play.api.libs.json._ library.

[{”device”:”Samsung S8”,”android”:true},
{”device”:”iPhone 8”,”android”:false},
{”device”:”MacBook Air Pro”,”android”:false},
{”device”:”Dell XPS”,”android”:false}]

I want to traverse through this json array in Scala. This array is assigned to var dependency. I want to get the device names which are android. How do I do that?

Upvotes: 3

Views: 16591

Answers (1)

Jose Romero
Jose Romero

Reputation: 569

You can try something like this:

val jsonString: String = "[{\"device\":\"Samsung S8\",\"android\":true {\"device\":\"iPhone8\",\"android\":false}, {\"device\":\"MacBook Air Pro\",\"android\":false},{\"device\":\"Dell XPS\",\"android\":false}]"
val jsonList: List[JsValue] = Json.parse(jsonString).as[List[JsValue]]
val filteredList: List[JsValue] = jsonList.filter(json => (json \ "android").as[Boolean])

Upvotes: 7

Related Questions