Skip to content Skip to sidebar Skip to footer

Decode Nested Variable-length Json In Elm

I'm having trouble wrapping my head around decoding a bit complex JSON response in ELM. The JSON response structure(I have no control over it) doesn't match my application's data t

Solution 1:

Json.Decode.index can be used to pull a specific index out of an array with the specified decoder. Couple that with andThen and map to do the attendance calculation based on multiple fields:

import Json.Decode exposing (..)

leaderDecoder : Decoder Leader
leaderDecoder =
    let
        sessionsAttendedDecoder =
            index 7 float
                |> andThen (\total -> index 8 float
                |> map (\attended -> (attended / total) * 100))
    in
        map3 Leader
            sessionsAttendedDecoder
            (index 2 string)
            (index 5 string)

Post a Comment for "Decode Nested Variable-length Json In Elm"