I am new to Scala and I need to figure out how to process a JSON depending on two scenarios.
First is where I receive such a JSON:
{..."field":[{"count":1,"value":"foo"}]...}
Second scenario is where I receive this kind of JSON: {..."field":"foo"...}
I can not determine when each JSON will come in(it is random). In both scenarios I need to get the field value and store as a String into separate JSON. Second scenario is obvious as it is a String already but I can not figure out how to determine whenever this field is Array or String and if Array then get the "foo" from JSON within it. I know how to access the field as it is a cursor:
val field = cursor.downField("foo").downField("bar").downField("lorem").downField("field")
Below example of my approach which is not working:
if (field.asInstanceOf[String]) {
rowFiltered.set("field", field.as[String])
} else {
rowFiltered.set("field", field.as[Seq[field[0]]])
}
Help with determining and assigning will be appreciated.
isInstanceOfrather thanasInstanceOfbut perhaps that is a typo.