Search code examples
scalaapiplayframeworkjsvalue

How to handle JsValue in Scala/Play


I'm trying to learn Scala/Play so I created a sample api that uses WSRequest to connect to GitHub and returns some info based on the user's id. I can convert this response to JsValue by doing:

val response: JsValeu = result.json
Ok(json)

I am having trouble when trying to manipulate the JsValue, for example filter values based on some criteria, etc? Do I need to convert it to a JsObject? I've looked on the Play documentation but I can't figure out how to do this.

What is the approach when handling JsValue?

Thanks


Solution

  • JsValue signifies any sort of JSON data entity, including objects, numbers, strings, etc.

    If you want to filter values in a JsObject, then you'll have to "cast" your JsValue into a JsObject. like:

    val jsonObject: JsObject = response.as[JsObject]

    Then you can mutate the object how you like.

    Read the documentation on JsObject and JsValue to find out how to do the rest of what you're trying to do.