3

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?

1 Answer 1

7

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])
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! This is what I needed!
What import needs to be done for the line Json.parse(jsonString).as[List[JsValue]]
The library is mentioned in the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.