Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 948
Aakanksha Choudhary Avatar asked Dec 16 '25 20:12

Aakanksha Choudhary


1 Answers

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])
like image 133
Jose Romero Avatar answered Dec 19 '25 19:12

Jose Romero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!