In this lesson, you will learn how to send JSON responses using Mizu. JSON is a common format for web apps and APIs to exchange data. Mizu provides a helper method that automatically converts your Go values to JSON and sets the correct headers. You will see how to return structured data from handlers in a clean and simple way.Documentation Index
Fetch the complete documentation index at: https://docs.go-mizu.dev/llms.txt
Use this file to discover all available pages before exploring further.
Code
Create a file namedmain.go and add this code:
Run
Run the server:curl:
-
http://localhost:8080/
Returns:
{"message":"Welcome to Mizu JSON API"} - http://localhost:8080/time Returns the current server time and a status field.
-
http://localhost:8080/user
Returns:
{"name":"Alice","email":"alice@example.com"}
How it works
Thec.JSON(statusCode, data) method converts any Go value to JSON, sets the Content-Type: application/json header, and sends the response.
You can return maps, slices, or structs. Mizu uses Go’s built-in JSON encoder, and you can use struct tags to define custom field names.
If encoding fails, Mizu sends an error response automatically. This makes it easy to build APIs that return structured data without handling JSON manually.
Try something new
Add another route that returns a list of posts:main():