Last active
August 29, 2015 14:22
-
-
Save Pyppe/4b2d60a8b0b9389d70e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Main extends App { | |
import play.api.libs.json._ | |
import play.api.libs.json.Reads._ | |
val json = Json.obj( | |
"foo" -> "bar", | |
"user" -> Json.obj( | |
"name" -> "Mike", | |
"age" -> 45, | |
"food" -> "pizza" | |
) | |
) | |
val userNameAndAge = (__ \ "user").json.update( | |
__.read[JsObject].map { user => | |
val fields = user.fields.filter { | |
case (field, _) => field == "name" || field == "age" | |
} // Seq((name,"Mike"), (age,45)) | |
JsObject(fields) | |
} | |
) | |
json.transform(userNameAndAge) | |
// Now... This yields: | |
// JsSuccess({"foo":"bar","user":{"name":"Mike","age":45,"food":"pizza"}},/user) | |
// ... instead of (and what I'd like to have): | |
// JsSuccess({"foo":"bar","user":{"name":"Mike","age":45}},/user) | |
// Why? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
proptip from @OlegYch: https://github.com/mandubian/play-json-zipper