# JSON Decoders - did you get all the fields?

**URL:** <https://discourse.elm-lang.org/t/json-decoders-did-you-get-all-the-fields/4166>\
**Category:** Show and Tell\
**Created:** [August 14, 2019, 3:17pm UTC](https://discourse.elm-lang.org/t/json-decoders-did-you-get-all-the-fields/4166 "2019-08-14T15:17:02Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![eike](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eike/32/749_2.png) [@eike](https://discourse.elm-lang.org/u/eike)\
**Post date:** [August 18, 2019, 11:52am UTC](https://discourse.elm-lang.org/t/json-decoders-did-you-get-all-the-fields/4166/5 "2019-08-18T11:52:43Z")

</div>

I had a similar problem recently, so I decided to create a small package for the problem this weekend: [eike/json-decode-complete](https://package.elm-lang.org/packages/eike/json-decode-complete/1.0.0/). It allows you to write object decoders that fail if you don’t handle all fields:

```auto
import Json.Decode exposing (Decoder)
import DecodeComplete exposing (..)

type alias User =
    { name : String
    , age : Int
    }

userDecoder : Decoder User
userDecoder =
    object User
        |> required "name" Decode.string
        |> required "age" Decode.int
        |> discard "email"
        |> complete

```

This decoder will fail if the provided JSON has fields other than `name`, `age` and `email`.

Unfortunately, my approach requires first decoding into a `Dict String Decode.Value` and then decoding the elements which degrades the error messages because I have to re-throw them with fail. Maybe there is a better way to implement this?

---

_[View the full topic](https://discourse.elm-lang.org/t/json-decoders-did-you-get-all-the-fields/4166)._
