summaryrefslogtreecommitdiff
path: root/json.go
blob: 6cb3af555dfc9f4f4f2e18b3a44d9b4624000198 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// This is a simple example
package main

import 	(
	"encoding/json"
)

// formatJSON takes an unformatted JSON string and returns a formatted version.
func formatJSON(unformattedJSON string) (string, error) {
	var jsonData interface{}

	// Decode the JSON string into an interface
	err := json.Unmarshal([]byte(unformattedJSON), &jsonData)
	if err != nil {
		return "", err
	}

	// Re-encode the JSON with indentation for formatting
	formattedJSON, err := json.MarshalIndent(jsonData, "", "    ")
	if err != nil {
		return "", err
	}

	return string(formattedJSON), nil
}