Crystal JSON — Parsing, Serialization, and Mapping
DodaTech
Updated 2026-06-29
1 min read
In this tutorial, you will learn about Crystal JSON. We cover key concepts, practical examples, and best practices to help you master this topic.
Crystal's JSON support is built into the standard library with compile-time mapping macros.
require "json"
# Mapping JSON to Crystal objects
class User
JSON.mapping(
name: String,
age: Int32,
email: String?,
roles: Array(String)
)
end
# Parse
json = %({"name":"Alice","age":30,"roles":["admin"]})
user = User.from_json(json)
puts user.name # => Alice
# Serialize
user = User.new
user.name = "Bob"
puts user.to_json # => {"name":"Bob","age":0,"roles":[]}
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro