mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-27 09:21:14 -06:00
25 lines
526 B
Go
25 lines
526 B
Go
package structure
|
|
|
|
import "encoding/json"
|
|
|
|
// Takes a value containing JSON string and passes it through
|
|
// the JSON parser to normalize it, returns either a parsing
|
|
// error or normalized JSON string.
|
|
func NormalizeJsonString(jsonString interface{}) (string, error) {
|
|
var j interface{}
|
|
|
|
if jsonString == nil || jsonString.(string) == "" {
|
|
return "", nil
|
|
}
|
|
|
|
s := jsonString.(string)
|
|
|
|
err := json.Unmarshal([]byte(s), &j)
|
|
if err != nil {
|
|
return s, err
|
|
}
|
|
|
|
bytes, _ := json.Marshal(j)
|
|
return string(bytes[:]), nil
|
|
}
|