Then can you deserialize into something with that structure? Then all the "missing fields" and "wrong types" kinds of problems are dealt with by the deserialization instead of you needing to write them yourself.
Maybe you could use something like #[derive(Deserialize)] struct Blah { traceEvents: Vec<JsonValue> }?
Alternatively, look for a json-path library that works with JsonValue, and make that do the traversal.
There are additional fields that might exist and are not interesting. Not sure if using a struct with only traceEvents fields will simplify the problem?
Combinators can help reduce indentation if you have one path of successful nesting:
pub fn get_trace_events(json_object: &JsonValue) -> Result<&Vec<JsonValue>, Box<dyn Error>> {
Ok(
json_value_as_object(json_object)
.ok_or(json::Error::WrongType("Expecting an object".to_string()))?
.get("traceEvents")
.ok_or(NotTraceEventError())?
)
}
// I didn't find a similar function in rust_json crate, so we have to implement this ourself
fn json_value_as_object(json_value: &JsonValue) -> Option<&Object> {
match(json_value) {
JsonValue::Object(object) => Some(object),
_ => None,
}
}