github.com/hs0210/hashicorp-terraform@v0.11.12-beta1/helper/structure/normalize_json.go (about)

     1  package structure
     2  
     3  import "encoding/json"
     4  
     5  // Takes a value containing JSON string and passes it through
     6  // the JSON parser to normalize it, returns either a parsing
     7  // error or normalized JSON string.
     8  func NormalizeJsonString(jsonString interface{}) (string, error) {
     9  	var j interface{}
    10  
    11  	if jsonString == nil || jsonString.(string) == "" {
    12  		return "", nil
    13  	}
    14  
    15  	s := jsonString.(string)
    16  
    17  	err := json.Unmarshal([]byte(s), &j)
    18  	if err != nil {
    19  		return s, err
    20  	}
    21  
    22  	bytes, _ := json.Marshal(j)
    23  	return string(bytes[:]), nil
    24  }