I have random JSON strings that contain string values when they are really numbers but I have no way to know what fields they might be when coming into my system.
For example:
{
"some_key":"1234",
"another_key":{
"sub_key":"4243",
"a_string":"this is a valid string and should not be converted"
}
}
Does any one know of a simple way to try to convert json values into integers using json4s?
Figured it out. This piece of code does just what I need:
private def convertNumbers(json: JValue) =
json.transformField {
case JField(k, v) ⇒ k → (v match {
case s: JString ⇒ Try(JInt(s.s.toInt)).getOrElse(s)
case o ⇒ o
})
}