JSON

Converts any interface or string to JSON using Jsoniter.

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