Get updates via , Github, or RSS | About

Episode 28: Json.Decode.Pipeline Mar 16, 2017

The core Json.Decode module has functions map through map8 which allow you to easily decode Json objects with up to eight properties, but if you want to decode more properties than that, things get a little tricky. Dealing with optional and nullable properties complicates things even further. Use No Red Ink’s Json.Decode.Pipeline to decode larger objects and better represent the underlying Json you’re parsing.

Examples

Main.elm

import Json.Decode.Pipeline exposing (decode, required, optional)


personDecoder : Decoder Person
personDecoder =
    decode Person
        |> required "username" string
        |> required "url" string
        |> required "bank" float
        |> required "lastBetSize" float
        |> required "handsPlayed" int
        |> required "handsWon" int
        |> required "averageBet" float
        |> required "lastPlayedAt" string
        |> optional "favoriteCasino" (maybe string) Nothing