Go JSON Marshal


   16 Apr 2024

Go’s json.Marshal and json.MarshalIndent functions both escape HTML data.

This may be useful for some circumstances when it is being output in a browser but it is not useful when your programs expects data without escaping.

Here are a couple of functions that Marshal JSON data without escaping HTML characters.

JSON_Marshal

func JSON_Marshal(input interface{}) ([]byte, error) {
	//------------------------------------------------------------
	var err error
	var encodeBuffer bytes.Buffer
	//------------------------------------------------------------
	encoder := json.NewEncoder(&encodeBuffer)
	encoder.SetEscapeHTML(false)
	//------------------------------------------------------------
	err = encoder.Encode(input)
	if err != nil {
		return nil, err
	}
	//------------------------------------------------------------
	return bytes.TrimRight(encodeBuffer.Bytes(), "\n"), err
	//------------------------------------------------------------
}

JSON_MarshalIndent

func JSON_MarshalIndent(input interface{}, prefix string, indent string) ([]byte, error) {
	//------------------------------------------------------------
	var err error
	var encodeBuffer bytes.Buffer
	var indentBuffer bytes.Buffer
	//------------------------------------------------------------
	encoder := json.NewEncoder(&encodeBuffer)
	encoder.SetEscapeHTML(false)
	//------------------------------------------------------------
	err = encoder.Encode(input)
	if err != nil {
		return nil, err
	}
	//------------------------------------------------------------
	err = json.Indent(&indentBuffer, bytes.TrimRight(encodeBuffer.Bytes(), "\n"), prefix, indent)
	if err != nil {
		return nil, err
	}
	//------------------------------------------------------------
	return indentBuffer.Bytes(), err
	//------------------------------------------------------------
}

Further Reading

More information about the encoding/json package can be found on the Go Website.

  Categories:   development   go   json
  Tags:   golang   go   json   marshal