JSON to Elixir Struct Generator

Generate Elixir defmodule with defstruct and @type t specifications from JSON data.

What is JSON to Elixir Struct Generator?

This tool converts JSON data into Elixir defmodule definitions with defstruct and @type t type specifications. It automatically infers Elixir types from JSON values, generates atom keys, and creates properly structured modules with nested struct support.

How to Use

  1. Set the module name prefix (e.g. MyApp.) for your Elixir modules
  2. Paste your JSON data into the input area
  3. Click "Generate" and copy the output into your Elixir project

Why Use This Tool?

Automatically infer Elixir types from JSON values (integer, String.t, boolean, float, list)
Generate defmodule with defstruct and @type t specifications
Handle nested objects as separate Elixir struct modules
Use atom keys following Elixir conventions
Generate children-first module ordering for proper compilation

Tips & Best Practices

  • Use realistic sample data — type inference depends on actual values
  • Set a proper module prefix matching your OTP app structure
  • Nested objects become separate modules with the prefix + PascalCase name
  • Null values are typed as nil | term() for flexibility

Frequently Asked Questions

How are JSON types mapped to Elixir?

JSON strings → String.t(), integers → integer(), floats → float(), booleans → boolean(), null → nil | term(), arrays → list() or [T], objects → separate nested struct modules with .t() type reference.

What is defstruct in Elixir?

defstruct defines a struct in Elixir, which is a tagged map with a fixed set of keys and default values. Structs provide compile-time guarantees and are the idiomatic way to define data models in Elixir.

How are nested objects handled?

Nested JSON objects are converted to separate Elixir modules with their own defstruct and @type t definitions. They are ordered children-first so that referenced modules are compiled before the modules that use them.

What is the module prefix for?

The module prefix sets the namespace for generated modules. For example, with prefix "MyApp.", a "Root" struct becomes "MyApp.Root". This follows Elixir convention of organizing modules under an OTP application namespace.

Is my data sent to a server?

No, all processing happens entirely in your browser. Your JSON data never leaves your device.

Related Tools