From 7460c8fa1e50b506d2165fdd9481da0fb0b86920 Mon Sep 17 00:00:00 2001 From: Eric Leblond Date: Sun, 22 Dec 2024 20:50:43 +0100 Subject: [PATCH] datajson: handle case with multiple format In some cases, the JSON array will contain different values type and the wanted key may be absent from some items. This patch fixes this use case by ignoring the data and just printing a message if ever the no item have been found. --- src/datajson.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/datajson.c b/src/datajson.c index cdd0e5e968a1..c85569aeaa7c 100644 --- a/src/datajson.c +++ b/src/datajson.c @@ -330,6 +330,7 @@ static int DatajsonLoadString(Dataset *set, char *json_key, char *array_key) fclose(fp); } else { json_t *json; + bool found = false; if (ParseJsonFile(set->load, &json, array_key) == -1) return -1; @@ -340,10 +341,13 @@ static int DatajsonLoadString(Dataset *set, char *json_key, char *array_key) json_t *key = GetSubObjectByKey(value, json_key); if (key == NULL) { - FatalErrorOnInit("Can't find expected key '%s' in object", json_key); + /* ignore error as it can be a working mode where some entries + are not in the same format */ continue; } + found = true; + const char *val = json_string_value(key); DataJsonType json = { .value = NULL, .len = 0 }; @@ -359,6 +363,12 @@ static int DatajsonLoadString(Dataset *set, char *json_key, char *array_key) } } json_decref(json); + + if (found == false) { + FatalErrorOnInit( + "No valid entries for key '%s' found in the file '%s'", json_key, set->load); + return -1; + } } THashConsolidateMemcap(set->hash);