Reputation: 3645
I use JacksMapper
to parse JSON strings to Map[String,String]
:
def parseJSON(line: String): Map[String, String] = {
JacksMapper.readValue[Map[String, String]](line)
}
For some JSON strings it throws the error:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token
In particular it happens with this string:
{"id":"123","name":"visited","category":"abc","x_ids":["220"]}
I assume that the problem is with "x_ids":["220"]
, while I expect the result as Map[String,String]
. So, in this case I would be interested to convert arrays to strings like "x_ids"->"220,230"
. How can I do it flexibly so that the solution would be adaptable to other possible fields that might be arrays in some cases?
EDIT: In my case I never have complex arrays that should be parsed with a static class. Only arrays of numbers or strings.
Upvotes: 0
Views: 1327
Reputation: 116620
This is because you claim all values are String
s, but x_ids
is a JSON Array, not JSON String. Two ways to go about it:
java.lang.Object
, in which case you get String
and List
sDeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS
in which case JSON Array with one (and only one!) element may be "unwrapped" to match expected type -- not 100% sure it works here, but in theory shouldUpvotes: 0
Reputation: 1634
You could do it in two steps:
That could give something like:
def parseJSON(line: String): Map[String, String] = {
JacksMapper.readValue[Map[String, Any]](line)
.mapValues {
case array: Iterable[Any] => array.mkString(", ")
case anyValue: Any => anyValue.toString
}
}
Upvotes: 1