I have a Json request body that represents four key-value pairs. I am attempting to deserialise the Json body (a JsValue
object) into a Map of Strings, Map[String, String]
, but I get the following error:
"JsUndefined("VALUE1" is not an object)".
Json example:
{
"key1":"VALUE1",
"key2":"VALUE2",
"key3":"VALUE3",
"key4":"VALUE4"
}
Deserialization example:
val jsonResult = request.body //type of JsValue
(jsonResult \ "KEY1" \ "KEY2" \ "KEY3" \ "KEY4").asOpt[Map[String, String]]
It seems that attempting to extract the key-value pairs with the above line does not work, and that doing them indivudually does actually work E.g:
val keyPair1 = (jsonResult \ "KEY1")
val keyPair2 = (jsonResult \ "KEY2")
Question
Is there a way to deserialize each key-value pair in the provided JSON, represented as a JsValue
, into a Map[String, String]
all at once?
Any help is appreciated.