JSON to Elixir
Paste a JSON sample. Get Elixir types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.
Open the tool with Elixir chosen
An example
This JSON:
{
"id": 42,
"name": "Ada Lovelace",
"active": true,
"joined": "2020-01-02T03:04:05Z",
"tags": ["engineer", "writer"],
"address": { "city": "London", "postcode": null },
"scores": [9.5, 8, 10]
}
becomes this Elixir:
defmodule Address do
@enforce_keys [:city, :postcode]
defstruct [:city, :postcode]
@type t :: %__MODULE__{
city: String.t(),
postcode: nil
}
end
defmodule Root do
@enforce_keys [:id, :name, :active, :joined, :tags, :address, :scores]
defstruct [:id, :name, :active, :joined, :tags, :address, :scores]
@type t :: %__MODULE__{
id: integer(),
name: String.t(),
active: boolean(),
joined: String.t(),
tags: [String.t()],
address: Address.t(),
scores: [float()]
}
end
With the reader and the writer
The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.
# This file was autogenerated using quicktype https://github.com/quicktype/quicktype
#
# Add Jason to your mix.exs
#
# Decode a JSON string: Root.from_json(data)
# Encode into a JSON string: Root.to_json(struct)
defmodule Address do
@enforce_keys [:city, :postcode]
defstruct [:city, :postcode]
@type t :: %__MODULE__{
city: String.t(),
postcode: nil
}
def from_map(m) do
%Address{
city: m["city"],
postcode: m["postcode"],
}
end
def from_json(json) do
json
|> Jason.decode!()
|> from_map()
end
def to_map(struct) do
%{
"city" => struct.city,
"postcode" => struct.postcode,
}
end
def to_json(struct) do
struct
|> to_map()
|> Jason.encode!()
end
end
defmodule Root do
@enforce_keys [:id, :name, :active, :joined, :tags, :address, :scores]
and 49 more lines. Open the tool to see the whole file.
What you need in your project
A JSON reader such as Jason.
The output is a struct for each object, which is the usual way to hold a known shape in Elixir.
The trap to know about
Elixir has no compile-time check on a map key, so a typo in a key is found only when the code runs.
The options you can change
These belong to Elixir, so the row of switches changes with the language.
| Option | What it does | Default |
|---|---|---|
| Types only | Plain types only | false |
| Namespace | Specify a module namespace | |
Questions
How do I turn JSON into Elixir?
Paste your JSON into the box, choose Elixir, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.
Does my JSON go to a server?
No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.
What do I need to add to my Elixir project?
A JSON reader such as Jason.
How does it know which fields are optional?
From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.
Other languages
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to Code