HackerDuck HackerDuck -5 years ago 120
JSON Question

Transform original Json using Play Framework in Scala

I have the following Json:

{"id": 1,

How can I transform it into
"url": "http://..."

How can I do this? I tried the following, but it does not seem to be working.

(json \ "value").as[JsString].value

Answer Source

Because "url" has an array value, you need to index it appropriately to get the value you require, so in your case it would be something like:

scala> val url = (json \ "url")(2).get
url: play.api.libs.json.JsValue = {"value":"http://..."}

scala> (url \ "value").as[String]
res22: String = http://...

I also recommend looking at Argonaut if you are comfortable with a more functional approach for dealing with JSON. It is much superior to the JSON facilities provided by Play.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download