# Understanding Decoder

**URL:** <https://discourse.elm-lang.org/t/understanding-decoder/4488>\
**Category:** Learn\
**Created:** [October 15, 2019, 6:56pm UTC](https://discourse.elm-lang.org/t/understanding-decoder/4488 "2019-10-15T18:56:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stoft](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stoft/32/753_2.png) [@stoft](https://discourse.elm-lang.org/u/stoft)\
**Post date:** [October 15, 2019, 6:56pm UTC](https://discourse.elm-lang.org/t/understanding-decoder/4488/1 "2019-10-15T18:56:01Z")

</div>

For a long time `Json.Decoder` was a mystery to me. I could use it but I just didn’t get how it worked, since e.g. `Decode.string` clearly returns a `Decoder String` and not a function how the heck does it know how to take a JSON value and decode it into a string? Or so I reasoned.

Not long ago we were working with form validations, which are very similar, and suddenly it clicked. A Decoder is simply hiding a function that takes a value and decodes it into the target value type. Or in Elm parlance:

`type Decoder a : jsonValue -> a`

Which is actually not valid Elm, but hopefully a correct understanding.

---

<div class="post-metadata">

**Author:** ![folkertdev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/folkertdev/32/133_2.png) [@folkertdev](https://discourse.elm-lang.org/u/folkertdev)\
**Post date:** [October 15, 2019, 9:09pm UTC](https://discourse.elm-lang.org/t/understanding-decoder/4488/2 "2019-10-15T21:09:07Z")

</div>

that’s broadly the idea, yes. You could re-implement `Decoder` like so

```elm
type Decoder a = Decoder (Json.Decode.Value -> Result String a)

succeed : a -> Decoder a
succeed x = 
    Decoder (\_ -> Ok x)

-- etc.

```

_you’d need access somehow to the internals of `Json.Decode.Value` as well_

So basically, it is a wrapper around `source -> Result error target`. The `Result` allows the decoding to fail. Types of this form have standard definitions for e.g. `map`, `map2` and `andThen`.

The same idea goes for `Parser ctx problem a` from `elm/parser`, `Decoder` from `elm/bytes`, really any kind of parsing or validation. It’s always nice when such a general pattern ‘clicks’.

---

<div class="post-metadata">

**Author:** ![lydell](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lydell/32/178_2.png) [@lydell](https://discourse.elm-lang.org/u/lydell)\
**Post date:** [October 16, 2019, 5:19am UTC](https://discourse.elm-lang.org/t/understanding-decoder/4488/3 "2019-10-16T05:19:46Z")

</div>

@stoft and I was actually working together on the form validations and shared the epiphany on how decoders work. Looks like I forgot to tell you that I posted about it 🙂 : [Elm/json re-implemented in Elm for learning](https://discourse.elm-lang.org/t/elm-json-re-implemented-in-elm-for-learning/4395). I should have mentioned you in that post, because without your realization it would never have been a thing!

---

<div class="post-metadata">

**Author:** ![joelq](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/joelq/32/445_2.png) [@joelq](https://discourse.elm-lang.org/u/joelq)\
**Post date:** [October 16, 2019, 2:24pm UTC](https://discourse.elm-lang.org/t/understanding-decoder/4488/4 "2019-10-16T14:24:19Z")

</div>

I’m a fan of this series of blog posts by Kofi Gumbs that walk through implementing your own `Json.Decode`

1. [Primitive decoders](https://8thlight.com/blog/kofi-gumbs/2017/06/28/elm-json-decoding-types.html)
2. [Object and array](https://8thlight.com/blog/kofi-gumbs/2017/07/17/elm-json-decoding-data-structures.html)
3. [`map` and `andThen`](https://8thlight.com/blog/kofi-gumbs/2017/08/29/elm-json-decoding-custom.html)

---

<div class="post-metadata">

**Author:** ![stoft](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stoft/32/753_2.png) [@stoft](https://discourse.elm-lang.org/u/stoft)\
**Post date:** [October 16, 2019, 2:47pm UTC](https://discourse.elm-lang.org/t/understanding-decoder/4488/5 "2019-10-16T14:47:57Z")

</div>

Dagnabbit, I completely missed that post (obviously)! Cool stuff!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [October 26, 2019, 2:48pm UTC](https://discourse.elm-lang.org/t/understanding-decoder/4488/6 "2019-10-26T14:48:00Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
