I want to use ujson of the upickle library to extract an optional string from a json document. In some documents the json field exists, in others not.
When acessing the field and the field does not exist I get a NoSuchElementException:
val json = ujson.read(jsonString)
json("attributename").str
results in: java.util.NoSuchElementException: key not found: attributename
What is the idiomatic way to deal with optional json attributes in ujson?
I am not sure, but, I was looking the GitHub repo here It seems at line 62 and from line 87 till line 99, it just call
x.obj(i).It doesn't perform any check, and just call it. This lead to a
java.util.NoSuchElementExceptionbecause trying to access without a check. I didn't see around any version to get the Option, or to even perform a check if this value exists.I would suggest to go via a Try/Success/Failure idiom on scala