Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to avoid the HTML characters replacing with unicode characters #103

Open
kaarthickkaran opened this issue Jun 21, 2021 · 3 comments
Open

Comments

@kaarthickkaran
Copy link

kaarthickkaran commented Jun 21, 2021

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
}
}

@mihaitodor
Copy link
Contributor

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 <script> tags, the string is encoded using HTMLEscape, which replaces "<", ">", "&", U+2028, and U+2029 are escaped to "\u003c","\u003e", "\u0026", "\u2028", and "\u2029". This replacement can be disabled when using an Encoder, by calling SetEscapeHTML(false).

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!

@kaarthickkaran
Copy link
Author

kaarthickkaran commented Jul 12, 2021

Hi @mihaitodor Sir,

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

@kevin-lindsay-1
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants