Teddy Dong Teddy Dong - 2 months ago 17
Scala Question

Scala Play Json JSResultException Validation Error

I am trying to pass this json string to my other method but SOMETIMES I get this error,

play.api.libs.json.JsResultException: JsResultException(errors:List((,List(ValidationError(error.expected.jsstring,WrappedArray())))))

I find it strange that this occurs randomly, sometimes I don't get the exception and sometimes I do. Any ideas?

Here is what my json looks like

val string = {
"first_name" : {
"type" : "String",
"value" : "John"
},
"id" : {
"type" : "String",
"value" : "123456789"
},
"last_name" : {
"type" : "String",
"value" : "Smith"
}
}


I read it like

(string \ "first_name").as[String]

Answer

(string \ "first_name") gives JsValue not JsString so as[String] does not work.

But if you need first name value you can do

val firstName = ((json \ "first_name") \ "value").as[String]