JSON to Elm
Paste a JSON sample. Get Elm types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.
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 Elm:
type alias Root =
{ id : Int
, name : String
, active : Bool
, joined : String
, tags : List String
, address : Address
, scores : List Float
}
type alias Address =
{ city : String
, postcode : ()
}
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.
-- To decode the JSON data, add this file to your project, run
--
-- elm install NoRedInk/elm-json-decode-pipeline
--
-- add these imports
--
-- import Json.Decode exposing (decodeString)
-- import QuickType exposing (root)
--
-- and you're off to the races with
--
-- decodeString root myJsonString
module QuickType exposing
( Root
, rootToString
, root
, Address
)
import Json.Decode as Jdec
import Json.Decode.Pipeline as Jpipe
import Json.Encode as Jenc
import Dict exposing (Dict)
type alias Root =
{ id : Int
, name : String
, active : Bool
, joined : String
, tags : List String
, address : Address
, scores : List Float
}
type alias Address =
{ city : String
, postcode : ()
}
-- decoders and encoders
rootToString : Root -> String
rootToString r = Jenc.encode 0 (encodeRoot r)
and 44 more lines. Open the tool to see the whole file.
What you need in your project
Nothing. `elm/json` comes with the compiler.
Elm needs a decoder for every type, written out by hand, and that is exactly the work this tool removes.
The file holds the type alias, the decoder and the encoder together.
The trap to know about
An Elm decoder fails on the whole document if one field is missing. Use the optional decoder the file gives you where the API is loose.
The options you can change
These belong to Elm, so the row of switches changes with the language.
| Option | What it does | Default |
|---|---|---|
| Types only | Plain types only | false |
| Array type | Use Array or List | array | list |
| Generated module name | Generated module name | QuickType |
Questions
How do I turn JSON into Elm?
Paste your JSON into the box, choose Elm, 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 Elm project?
Nothing. `elm/json` comes with the compiler.
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