Skip to content
XUtilHub

JSON to Elixir Struct Generator

Convert JSON to Elixir structs with @type specs, Jason.Encoder derivation and from_map/1 functions that build nested structs from decoded JSON.

  • Free, no sign-up
  • Runs in your browser, nothing uploaded
Input
Code

About the JSON to Elixir

Generate Elixir modules with defstruct, typespecs and a from_map/1 function that turns Jason-decoded maps into nested structs.

Key features

  • One module per JSON object with defstruct
  • @type t specs for Dialyzer
  • from_map/1 builds nested structs from decoded JSON
  • @derive Jason.Encoder for encoding back to JSON
  • snake_case atom keys
  • Optional module prefix (e.g. MyApp.Schemas)

Elixir structs from JSON

Phoenix apps, background workers and API clients in Elixir decode JSON into maps with string keys. Structs make that data explicit, with compile-time checks for keys, typespecs for Dialyzer, and pattern matching. Generate them from a sample in one click.

Example

{ "id": 1001, "price": 9.5, "tags": ["gift"], "customer": { "name": "Ada" }, "coupon": null }
defmodule Order do
  @moduledoc false
  @derive Jason.Encoder
  defstruct [:id, :price, :tags, :customer, :coupon]

  @type t :: %__MODULE__{
          id: integer(),
          price: float(),
          tags: [String.t()],
          customer: Customer.t(),
          coupon: any()
        }

  @spec from_map(map()) :: t()
  def from_map(map) when is_map(map) do
    %__MODULE__{
      id: map["id"],
      price: map["price"],
      tags: map["tags"],
      customer: Customer.from_map(map["customer"]),
      coupon: map["coupon"]
    }
  end
end
order = json |> Jason.decode!() |> Order.from_map()
order.customer.name

Type mapping

JSONTypespec
"text"String.t()
42integer()
3.14float()
trueboolean()
[...][t]
{...}Module.t()
null or missingt | nil
unknownany()

Tips

  • Module prefix: set it to your app namespace (e.g. MyApp.Schemas) to avoid clashes with other modules.
  • Required keys: add @enforce_keys [:id] for fields that must always be present.
  • Validation: wrap from_map with your own checks, or move to Ecto embedded schemas for full casting.

Generating for other dynamic languages? See JSON to Ruby and JSON to Python.

How to use the JSON to Elixir

  1. Paste your data into the input box, upload a file, or click Sample to try it out.
  2. Adjust the options above the editor if you need to (indentation, delimiter, and so on).
  3. The Elixir appears instantly in the output panel. Any errors are shown with their line and column.
  4. Click Copy to copy the result, or Download to save it as a file.

Frequently asked questions

How do I use the modules?

Decode with Jason and pass the map to from_map: json |> Jason.decode!() |> MyApp.Order.from_map().

Why from_map instead of decoding with keys: :atoms?

Converting untrusted JSON keys to atoms can exhaust the atom table. from_map reads string keys and builds structs with predefined atom keys, which is safe.

Should I use Ecto embedded schemas instead?

For validation and casting, Ecto embedded schemas with changesets are a great choice. These plain structs are lighter when you just need typed data.

What does @derive Jason.Encoder do?

It lets Jason.encode!/1 serialize the struct back to JSON. Untick the option if you don't use Jason.

Is my JSON uploaded to a server?

No. All JSON tools run entirely in your browser, so your data never leaves your device.

Is there a size limit?

There is no fixed limit. Files of several megabytes work fine; very large files depend on your device's memory.

Do I need an account?

No. Every JSON tool is free and works without signing up.

Last updated:

Related JSON tools