tidwall / sjson

Set JSON values very quickly in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

add option to compact result json

luisdavim opened this issue · comments

Hi, it would be cool to have a way to get the resulting json compacted.

sjson is designed to make changes to the json as quickly as possible, which means it doesn't concern itself with reformatting the json.

I recommend the pretty package.

package main

import (
	"github.com/tidwall/pretty"
	"github.com/tidwall/sjson"
)

func main() {
	json := `{
		"hello": "jello"
	}`
	json, _ = sjson.Set(json, "hello", "pudding")
	json = string(pretty.Ugly([]byte(json)))
	println(json)
}

// output: {"hello":"pudding"}

Or, the gjson @ugly modifier.

package main

import (
	"github.com/tidwall/gjson"
	"github.com/tidwall/sjson"
)

func main() {
	json := `{
		"hello": "jello"
	}`
	json, _ = sjson.Set(json, "hello", "pudding")
	json = gjson.Get(json, "@ugly").Raw
	println(json)
}

// output: {"hello":"pudding"}