We are there. Finally. Decoding Json. Things we have learned up til now are will culminate into fetching Json data from a server endpoint, and transforming that into type safe Elm to be able to use in our Elm applications.
But first if we google Elm decoding json, we get these gems
The Json.Decode.decodeString function returns a Result. It has a type of:
decodeString : Decoder val -> String -> Result String val
so what does that mean? Well, more or less that we need Json.Decode to convert the JSON values into Elm values. And by elm values I mean, types safe, friendly compiler, you are there when I assume you are, and if not, it’s handled accordingly by type values.
> decodeString bool "true" Ok True : Result.Result String Bool > decodeString bool "false" Ok False : Result.Result String Bool
> import Json.Decode exposing (list, bool, string, int) > list <function> : Json.Decode.Decoder a -> Json.Decode.Decoder (List a) > bool <decoder> : Json.Decode.Decoder Bool > string <decoder> : Json.Decode.Decoder String
So what about really JSON, like arrays, objects, or arrays of objects!!!!! The idea would be to coralate an array of things to a thing of particular things, right?
decoder : Decoder String decoder = field "email" string
So great. That’s a single field. When was the last time our json payload returned us a single field. ummm, close to never. lol
Insert Pipeline to the rescue!
elm-package install NoRedInk/elm-decoder-pipeline learned that it’s case sensitive!!
This chapter we’ll refer to as a glut for punishment. For me, often if I am forced to do things the hard way, and then introduce a ‘simplier’ or more conventent shorthand way of doing something I tend to remember it better. So I skipped over the pipeline part and we straight to http.get instead of http.getString.
hour or two later…The struggle is real. google it
map2-map3 is a function of Decoder… NOT List. lol type mismatch. See what I mean about learning by just pounding on it.
I am a hammer, every book, my nail
So sweet jesus, optional fields, what have I gotten myself into. I have to note, at this point, I am strongly considering
git reset --hard and just taking the red pill and following pipeline.
So it turns out there is a plethera of methods to expose from Json.Decode. And we haven’t even gotten to Fancy Decoding!!!!
So which is a more Elm convention ? Maybe.withDefault, or separate function with a case expression ?
viewThumbnail : Maybe String -> Photo -> Html Msg viewThumbnail selectedUrl thumbnail = img [ src (urlPrefix ++ thumbnail.url) -- , hasTitle thumbnail.title thumbnail.size , title ((Maybe.withDefault "None" thumbnail.title) ++ " [" ++ toString thumbnail.size ++ " KB]") , classList [ ( "selected", selectedUrl == Just thumbnail.url ) ] , onClick (SelectByUrl thumbnail.url) ] 
hasTitle : Maybe String -> Int -> Attribute msg hasTitle maybeTitle size = case maybeTitle of Nothing -> title “None”
Just photoTitle -> title (photoTitle ++ " [" ++ toString size ++ " KB]")