Skip to main content
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.

Code

Create a file named main.go and add this code:

Run

Run the server:
Then open these links in your browser or use curl:

How it works

The c.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:
Then register it in main():
Visit http://localhost:8080/posts to see the JSON array. Now you can send structured JSON data from any route with just one line of code. Next, continue to Context and Lifecycle to learn how Mizu manages requests, deadlines, and cleanup.