How to format a json file the same as gofmt would?

I have the following json file:

{
  "signup": {
    "title": "Signup Page",
    "formName": "Signup Form",
    "name": "Name"
  },
  "login": {
    "title": "Login Page",
    "formName": "Login Form"
  }
}

But I want this:

{
	"signup": {
		"title":    "Signup Page",
		"formName": "Signup Form",
		"name":     "Name",
	},
	"login": {
		"title":    "Login Page",
		"formName": "Login Form",
	}
}

How to achieve that? I was searching and all the available options I found format the same as my first example.

Experiment with json.MarshalIndent or json.Indent

Maybe you can use a tool called jq GitHub - jqlang/jq: Command-line JSON processor

@jauhararifin, or the Golang version for a simpler install, GitHub - itchyny/gojq: Pure Go implementation of jq. It doesn’t have 100% parity with jq, but I think all the dissimilarities are documented.

1 Like