Advertisement

JSON to Elixir Struct

Generate Elixir structs from JSON with defstruct and typespecs. Nested modules, atom keys, and nilable fields are inferred automatically.

JSON input

Paste JSON, optionally name the root type, then generate.

Your Elixir struct code will appear here.
Advertisement

Structs from schemaless JSON

JSON has no schema, but Elixir structs demand one. The generator derives that schema from your sample: every object becomes a module with defstruct, every field gets a typespec, and shaky fields include nil().

Built for Phoenix APIs

Phoenix applications constantly translate between JSON and structs. Generating the struct layer from a real payload gives you a typed foundation for contexts, changesets, and JSON views.

JSON to Elixir FAQs

What is a defstruct?
defstruct defines a struct: a map with a fixed set of atom keys and compile time guarantees. It is how Elixir models domain entities like users and orders.
How are nested objects handled?
Each nested object becomes its own submodule with its own defstruct, so the module tree mirrors the JSON tree, which is the standard Elixir layout.
What do the typespecs add?
The @type annotations document each field's type for Dialyzer and editors. Fields that may be null get nil() in their union, making absence explicit.
How do I decode JSON into these?
Use Jason.decode! with keys: :atoms, then struct(User, map). For nested data, map each level or use a library like Ecto embedded schemas.
Are atom keys safe here?
The generator only creates atoms from keys present in your sample at generation time, which is the safe pattern. Never convert arbitrary user input to atoms at runtime.
Advertisement