JSON

Converts any interface or string to JSON using Jsoniter.

JSON also sets the content header to application/json.
  1. func (c *Ctx) JSON(data interface{}) error
  1. type SomeStruct struct {
  2. Name string
  3. Age uint8
  4. }
  5. app.Get("/json", func(c *fiber.Ctx) error {
  6. // Create data struct:
  7. data := SomeStruct{
  8. Name: "Grame",
  9. Age: 20,
  10. }
  11. return c.JSON(data)
  12. // => Content-Type: application/json
  13. // => "{"Name": "Grame", "Age": 20}"
  14. return c.JSON(fiber.Map{
  15. "name": "Grame",
  16. "age": 20,
  17. })
  18. // => Content-Type: application/json
  19. // => "{"name": "Grame", "age": 20}"
  20. })