Beatify the output again. Like for nodeinfo responses

This commit is contained in:
Melroy van den Berg 2025-04-25 01:42:53 +02:00
parent e56d6c9c41
commit 7fe3ce2234
No known key found for this signature in database
GPG key ID: 71D11FF23454B9D7

View file

@ -1,6 +1,7 @@
package formatter
import (
"encoding/json"
"fmt"
"strings"
"time"
@ -16,8 +17,21 @@ func Format(jsonData []byte) (string, error) {
// Create a summary based on the object type
summary := createSummary(jsonData)
// This might look unnecessary, but it is not in order to beautify the JSON.
// First Unmarkshall to get a map[string]interface{}
var data map[string]interface{}
if err := json.Unmarshal(jsonData, &data); err != nil {
return "", fmt.Errorf("error parsing JSON: %v", err)
}
// Marshal with indentation to beautify the output
pretty, err := json.MarshalIndent(data, "", " ")
if err != nil {
return "", fmt.Errorf("error beautifying JSON: %v", err)
}
// Combine the full JSON first, followed by the summary at the bottom
result := fmt.Sprintf("%s\n\n%s", string(jsonData), summary)
result := fmt.Sprintf("%s\n\n%s", pretty, summary)
return result, nil
}