Schemas for JSON optimized for code generation

JSON Type Definition, aka RFC 8927, is an easy-to-learn, portable, and standardized way to describe the shape of your JSON data.

  "properties": {
    "id": { "type": "string" },
    "createdAt": { "type": "timestamp" },
    "karma": { "type": "int32" },
    "isAdmin": { "type": "boolean" }
export interface User {
  createdAt: string;
  id: string;
  isAdmin: boolean;
  karma: number;
package user

import "time"

type User struct {
	CreatedAt time.Time `json:"created_at"`
	ID        string    `json:"id"`
	IsAdmin   bool      `json:"isAdmin"`
	Karma     int32     `json:"karma"`
package com.example;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.time.OffsetDateTime;

public class User {
    private OffsetDateTime createdAt;

    private String id;

    private Boolean isAdmin;

    private Integer karma;

    public User() {

    public OffsetDateTime getCreatedAt() {
        return this.createdAt;

    public void setCreatedAt(OffsetDateTime createdAt) {
        this.createdAt = createdAt;

    public String getId() {

    public void setId(String id) { = id;

    public Boolean getIsAdmin() {
        return this.isAdmin;

    public void setIsAdmin(Boolean isAdmin) {
        this.isAdmin = isAdmin;

    public Integer getKarma() {
        return this.karma;

    public void setKarma(Integer karma) {
        this.karma = karma;
using System;
using System.Text.Json.Serialization;

namespace Contoso.Example
    public class User
        public DateTimeOffset CreatedAt { get; set; }

        public string Id { get; set; }

        public bool IsAdmin { get; set; }

        public int Karma { get; set; }
from dataclasses import dataclass
from typing import Any, Optional, Union, get_args, get_origin

class User:
    created_at: 'str'
    id: 'str'
    is_admin: 'bool'
    karma: 'int'

    def from_json(cls, data) -> "User":
        Construct an instance of this class from parsed JSON data.

        return cls(
            _from_json(str, data.get("createdAt")),
            _from_json(str, data.get("id")),
            _from_json(bool, data.get("isAdmin")),
            _from_json(int, data.get("karma")),

    def to_json(self):
        Generate JSON-ready data from an instance of this class.

        out = {}
        out["createdAt"] = _to_json(self.created_at)
        out["id"] = _to_json(
        out["isAdmin"] = _to_json(self.is_admin)
        out["karma"] = _to_json(self.karma)
        return out

def _from_json(cls, data):
    if data is None or cls in [bool, int, float, str, object] or cls is Any:
        return data
    if get_origin(cls) is Union:
        return _from_json(get_args(cls)[0], data)
    if get_origin(cls) is list:
        return [_from_json(get_args(cls)[0], d) for d in data]
    if get_origin(cls) is dict:
        return { k: _from_json(get_args(cls)[1], v) for k, v in data.items() }
    return cls.from_json(data)

def _to_json(data):
    if data is None or type(data) in [bool, int, float, str, object]:
        return data
    if type(data) is list:
        return [_to_json(d) for d in data]
    if type(data) is dict:
        return { k: _to_json(v) for k, v in data.items() }
    return data.to_json()
# You can optionally generate .rbs too! (scroll to botton)

require 'json'
require 'time'

module Example
  class User
    attr_accessor :created_at
    attr_accessor :id
    attr_accessor :is_admin
    attr_accessor :karma

    def self.from_json_data(data)
      out =
      out.created_at = Example::from_json_data(DateTime, data["createdAt"]) = Example::from_json_data(String, data["id"])
      out.is_admin = Example::from_json_data(TrueClass, data["isAdmin"])
      out.karma = Example::from_json_data(Integer, data["karma"])

    def to_json_data
      data = {}
      data["createdAt"] = Example::to_json_data(created_at)
      data["id"] = Example::to_json_data(id)
      data["isAdmin"] = Example::to_json_data(is_admin)
      data["karma"] = Example::to_json_data(karma)


  def self.from_json_data(type, data)
    if data.nil? || [Object, TrueClass, Integer, Float, String].include?(type)
    elsif type == DateTime
    elsif type.is_a?(Array) { |elem| from_json_data(type.first, elem) }
    elsif type.is_a?(Hash)
      data.transform_values { |elem| from_json_data(type.values.first, elem) }

  def self.to_json_data(data)
    if data.nil? || [TrueClass, FalseClass, Integer, Float, String].include?(data.class)
    elsif data.is_a?(DateTime)
    elsif data.is_a?(Array) { |elem| to_json_data(elem) }
    elsif data.is_a?(Hash)
      data.transform_values { |elem| to_json_data(elem) }

# If you enable it, you'll also get a .rbs file:

module Example
  class User
    attr_accessor created_at: DateTime
    attr_accessor id: String
    attr_accessor is_admin: bool
    attr_accessor karma: Integer

    def self.from_json_data: (untyped) -> User
    def to_json_data: () -> untyped

  def self.from_json_data: (untyped, untyped) -> untyped
  def self.to_json_data: (untyped) -> untyped
use chrono::{DateTime, FixedOffset};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct User {
    #[serde(rename = "createdAt")]
    pub createdAt: DateTime<FixedOffset>,

    #[serde(rename = "id")]
    pub id: String,

    #[serde(rename = "isAdmin")]
    pub isAdmin: bool,

    #[serde(rename = "karma")]
    pub karma: i32,

Use JSON Typedef with the language of your choice

Why JSON Typedef?

A better way to work with JSON

JSON Type Definition is a lightweight schema language for JSON data. Describe the shape of your data once, and get portable validators and types across many languages.

Tiny and Easy to Learn

Most developers can learn the entire JSON Typedef specification in about five to ten minutes.

Portable and Specific Errors

Specific validation errors are part of the JTD spec. Every implementation returns the same errors.

Powerful Code Generation

The jtd-codegen tool can consistently generate code in many programming languages from any schema.

Easy Onboarding

Want to add JSON Typedef to an existing system? jtd-infer can generate a schema from examples of your data.

Mocking and Fuzzing

Once you have a schema, jtd-fuzz can generate mocks of your data. Generate seed data or load-testing workloads with ease.

Easy to Embed

JSON Typedef schemas are just plain old JSON, so you can embed them in your organization's JSON (or YAML) custom tools.

Ready to be more productive with JSON? Dig into the docs.