gabs icon indicating copy to clipboard operation
gabs copied to clipboard

How to avoid the HTML characters replacing with unicode characters

Open kaarthickkaran opened this issue 4 years ago • 3 comments

Hi,

am new to GO, I want to form a JSON for an item, so using this module I formed the JSON, but the angular brackets were replaced with Unicode characters. I want to know how to avoid the replacement of the Unicode character.

Code: package main

import ( "fmt" "github.com/Jeffail/gabs/v2" )

func main() { jsonObj := gabs.New() jsonObj.SetP("protein", "product.name") jsonObj.SetP("eg.", "product.description") jsonObj.SetP(1000, "product.price") fmt.Println(jsonObj.StringIndent("", " "))

}

Actual Output: { "product": { "description": "eg.\u003cthis product \u003e ", "name": "protein", "price": 1000 } }

Expected Output: { "product": { "description": "eg.<this product>", "name": "protein", "price": 1000 } }

kaarthickkaran avatar Jun 21 '21 11:06 kaarthickkaran

Hey @kaarthickkaran, thanks for trying out gabs! The issue you bumped into is due to the way in which the standard Go JSON lib marshals JSON as documented here: https://golang.org/pkg/encoding/json/#Marshal

String values encode as JSON strings coerced to valid UTF-8, replacing invalid bytes with the Unicode replacement rune. So that the JSON will be safe to embed inside HTML

Here is one way to work around this:

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"

	"github.com/Jeffail/gabs/v2"
)

func main() {
	jsonObj := gabs.New()
	jsonObj.SetP("protein", "product.name")
	jsonObj.SetP("eg.<this product>", "product.description")
	jsonObj.SetP(1000, "product.price")

	buf := bytes.Buffer{}
	encoder := json.NewEncoder(&buf)
	encoder.SetEscapeHTML(false)
	encoder.SetIndent("", " ")
	if err := encoder.Encode(jsonObj.Data()); err != nil {
		log.Fatalf("Failed to encode: %s", err)
		return
	}

	fmt.Println(buf.String())
}

The output of the above code is:

{
 "product": {
  "description": "eg.<this product>",
  "name": "protein",
  "price": 1000
 }
}

Hope this helps!

mihaitodor avatar Jul 11 '21 23:07 mihaitodor

Hi @mihaitodor Sir,

Thank you so much for the solution sir. Let me try this.

kaarthickkaran avatar Jul 12 '21 04:07 kaarthickkaran

If anybody is seeing escaped UTF-8 characters like this when trying to extract a string from parsed JSON, please try the following:

// escapes string
value := parsedJSON.Path("value").String()

// does not escape string
value, ok := parsedJSON.Path("value").Data().(string)

kevin-lindsay-1 avatar Jul 25 '22 15:07 kevin-lindsay-1