Keep Elm JSON Decoding at the Application Boundary
Parse external JSON once at the edge of your Elm app so your UI works with domain types instead of scattered Maybe checks.
09 Aug 2025, 21:32 UTC

Start at the edge, not in the view
Your Elm app fetches a user profile from an API. The JSON arrives as a string, but your view needs a name, an email, and a list of roles. It is tempting to decode just enough to satisfy the compiler, then pass Maybe String or Result String String through your update and view functions. Soon every component is checking whether a field exists, and the UI logic is mixed with parsing logic.
The practical alternative is to treat JSON as an external format and decode it once, at the application boundary—typically inside an Http.send task or a port handler. Once decoding succeeds, the rest of your program works with Elm domain types, not with JSON-shaped uncertainty.
What a decoder actually does
A Json.Decode.Decoder a is a description of how to turn a JSON value into an Elm value of type a. Elm 0.19.x requires you to declare this shape explicitly. The compiler will not let you ignore a decode failure: Json.Decode.decodeString returns Result Json.Decode.Error a, and you must handle both Ok and Err.
Common primitives include:
field— extracts a named key from a JSON object.list— decodes a JSON array into an Elm list.nullable— accepts a JSON null or a value decoded by another decoder.mapandmap4— combine several field decoders into a custom type or record.andThen— chains a second decoder based on the result of the first.
Because decoders are values, you can test them in isolation before wiring them to HTTP.
Worked example: a user profile decoder
Assume the API returns this payload:
{
"id": 7,
"name": "Ada Lovelace",
"email": "[contact removed]",
"roles": ["developer", "reviewer"]
}
First, confirm you are using Elm 0.19.x, because decoder APIs changed between 0.18 and 0.19. Run this in your project terminal:
elm --version
No special permissions are required. Expect output beginning with 0.19.. If you see a different major version, the examples below may not compile.
Next, define a domain type and a decoder in src/User.elm:
module User exposing (User, userDecoder)
import Json.Decode exposing (..)
type alias User =
{ id : Int
, name : String
, email : String
, roles : List String
}
userDecoder : Decoder User
userDecoder =
map4 User
(field "id" int)
(field "name" string)
(field "email" string)
(field "roles" (list string))
Then decode a string, for example inside an HTTP task or a port subscription:
decodeUser : String -> Result Decode.Error User
decodeUser json =
decodeString userDecoder json
You can verify the behavior in elm repl without starting the full app. Paste a valid payload and expect Ok; omit the email field and expect Err. The compiler will also catch a missing field at build time if your decoder expects it but your record construction does not match.
Run elm make src/Main.elm to confirm the module compiles. If you rename a JSON field but forget to update the decoder, elm make reports a type mismatch before the code ever reaches a browser. This is the main safety benefit: the failure happens at compile time or at the boundary, not deep inside a view function.
Trade-offs to keep in mind
Decoder code is more verbose than dynamic JSON parsing. A simple endpoint still needs an explicit record type and a decoder value, which adds initial implementation time. Decoding also validates structure only, not business rules. A decoder can confirm that age is an integer, but it will not confirm that the age is positive or that an email belongs to an active user. You still need domain validation after decoding succeeds.
Finally, do not ignore the Result. Pattern matching on Ok and Err at the boundary lets you convert a decode failure into a user-facing message or a retry, rather than letting an unhandled error propagate through the application.
Check your boundary
Pick one external endpoint in your app and ask: does the code that receives the JSON convert it to a domain type immediately? If your update function contains Maybe.map on API fields, move that logic into a Json.Decode value and call decodeString where the response arrives. Then run elm make and elm repl with a deliberately broken payload to confirm the error path is handled.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.