Minor: JsonUtils.extractValue shouldn't throw exceptions (#18554)

This commit is contained in:
Sriharsha Chintalapani 2024-11-09 09:45:07 -08:00 committed by GitHub
parent 9d9eccedda
commit a03d915909
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -517,14 +517,17 @@ public final class JsonUtils {
public static <T> T extractValue(String jsonResponse, String... keys) {
JsonNode jsonNode = JsonUtils.readTree(jsonResponse);
// Traverse the JSON structure using keys
for (String key : keys) {
jsonNode = jsonNode.path(key);
}
// Extract the final value
return JsonUtils.treeToValue(jsonNode, (Class<T>) getValueClass(jsonNode));
if (jsonNode.isMissingNode() || jsonNode.isNull()) {
return null;
}
try {
return JsonUtils.treeToValue(jsonNode, (Class<T>) getValueClass(jsonNode));
} catch (Exception e) {
return null;
}
}
public static <T> T extractValue(JsonNode jsonNode, String... keys) {