diff --git a/helper/flattenJson.go b/helper/flattenJson.go new file mode 100644 index 0000000000000000000000000000000000000000..4cd19fcd0bfa9f23210040a61724067fde43cc7e --- /dev/null +++ b/helper/flattenJson.go @@ -0,0 +1,21 @@ +package helper + +func FlattenJSON(prefix string, input map[string]interface{}, output map[string]interface{}) { + for key, value := range input { + // Build the new key + newKey := key + if prefix != "" { + newKey = prefix + "_" + key + } + + // Check the type of value + switch v := value.(type) { + case map[string]interface{}: + // If it's a nested JSON object, recursively flatten it + FlattenJSON(newKey, v, output) + default: + // Otherwise, add it to the output map + output[newKey] = v + } + } +} diff --git a/main.go b/main.go index 05162d374eeab01d62ca42953eff740e53857681..c5305474ac5b52788b103c958b9fc2929b6d63a4 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ func main() { log.Init() queue.Init() - if os.Getenv("OUTPUT_DATABSE") != "" && os.Getenv("CLIENT_USE_SERVER") != "true" { + if (os.Getenv("OUTPUT_DATABSE") != "" && os.Getenv("CLIENT_USE_SERVER") != "true") || os.Getenv("LOGGER_SERVER") == "true" { database.Init() if os.Getenv("LOGGER_SERVER") != "true" { database.CreateInstance() diff --git a/messageworker/client.go b/messageworker/client.go index b86403e012c87a4870fddb29f238ca1fd3d4da39..bac2ce00d96335024641f264278591d367f04b44 100644 --- a/messageworker/client.go +++ b/messageworker/client.go @@ -98,6 +98,8 @@ func runDbClient() { var jsonData map[string]interface{} err = json.Unmarshal([]byte(task.Data), &jsonData) + flattenedData := make(map[string]interface{}) + helper.FlattenJSON("", jsonData, flattenedData) if err != nil { log.Log.Error("Failed decoding boiler JSON: ", err.Error()) queue.MainQueue.Enqueue(taskData, 60) @@ -105,7 +107,7 @@ func runDbClient() { continue } valuesToInsert := []tables.Value{} - for key, value := range jsonData { + for key, value := range flattenedData { valueType := database.GetOrCreateValueType(key) dbValue := tables.Value{ Date: task.Date,