I have a Gin-Gonic REST API in Golang. Here I am trying to output users that are already registered as JSON, currently in Postman I only get that:
(You can ignore the lastRequest
-Attribut, because it is currently always nil)
"[{\"id\":\"e61b3ff8-6cdf-4b23-97a5-a28107c57543\",\"firstname\":\"John\",\"lastname\":\"Doe\",\"email\":\"[email protected]\",\"username\":\"johndoe\",\"token\":\"19b33c79-32cc-4063-9381-f2b64161ad8a\",\"lastRequest\":null},
But I want it like this:
[{
"id": "e61b3ff8-6cdf-4b23-97a5-a28107c57543",
"username": "johndoe",
"email": "[email protected]",
"token": "19b33c79-32cc-4063-9381-f2b64161ad8a",
"lastRequest": null
}]
How do I manage this, I tried many things with the 'json.MarshalIndent' (from this stackoverflow-post), however it didn't change anything for me, what do I need to do? Because the backslashes stay no matter what I do, at most \t
or spaces are inserted. I have also read that I have to do it as a byte array, but that didn't work for me either (maybe I did something wrong here too).
Here my current code:
var users []User
r.GET("/getusers", func(c *gin.Context) {
usersArr := make([]User, len(users))
for i := 0; i < len(users); i {
usersArr[i] = users[i]
}
userJson, err := json.Marshal(testUser)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
} else {
c.JSON(200, string(userJson))
}
})
type User struct {
Id string `json:"id"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Email string `json:"email"`
Username string `json:"username"`
Token string `json:"token"`
LastRequest []lastRequest `json:"lastRequest"`
}
CodePudding user response:
Two things:
- The "backslashes" are just escape characters. They aren't literally there.
- Indenting JSON is a simple matter of calling the appropriate Indent function:
If you have the JSON string already, use the json.Indent
function from the encoding/json
package:
input := []byte("[{\"id\":\"e61b3ff8-6cdf-4b23-97a5-a28107c57543\",\"firstname\":\"John\",\"lastname\":\"Doe\",\"email\":\"[email protected]\",\"username\":\"johndoe\",\"token\":\"19b33c79-32cc-4063-9381-f2b64161ad8a\",\"lastRequest\":null}]")
buf := &bytes.Buffer{}
if err := json.Indent(buf, input, "", "\t"); err != nil {
panic(err)
}
fmt.Println(buf.String())
However, if you're trying to marshal directly into the indented form, just use the MarshalIndent
function intead of Marshal
:
userJson, err := json.MarshalIndent(testUser, "", "\t")
CodePudding user response:
Well this would be my first answer in stackoverflow, hope this helps.
Go gin framework comes with a few handy functions, since I've got no idea what version of golang nor Gin Framework you're using you could try this:
var users []User
r.GET("/getusers", func(c *gin.Context) {
usersArr := make([]User, len(users))
for i := 0; i < len(users); i {
usersArr[i] = users[i]
}
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
} else {
c.JSON(200, users)
// If Indentation is required you could try:
c.IndentedJSON(200, users)
}
})
type User struct {
Id string `json:"id"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Email string `json:"email"`
Username string `json:"username"`
Token string `json:"token"`
LastRequest []lastRequest `json:"lastRequest"`
}