# Decode json object with warning for unhandled keys

**URL:** <https://discourse.elm-lang.org/t/decode-json-object-with-warning-for-unhandled-keys/1133>\
**Category:** Request Feedback\
**Created:** [April 16, 2018, 2:54pm UTC](https://discourse.elm-lang.org/t/decode-json-object-with-warning-for-unhandled-keys/1133 "2018-04-16T14:54:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rofrol](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rofrol/32/60_2.png) [@rofrol](https://discourse.elm-lang.org/u/rofrol)\
**Post date:** [April 16, 2018, 2:54pm UTC](https://discourse.elm-lang.org/t/decode-json-object-with-warning-for-unhandled-keys/1133/1 "2018-04-16T14:54:40Z")

</div>

This is my attempt to decode keys but get warning when some key is not handled. Full example [https://ellie-app.com/mxkGY5DVFa1/0](https://ellie-app.com/mxkGY5DVFa1/0).

Running this code will give this in browser console: `[Warning]: I ran into a`fail`decoder: Unhandled field somefield: { url = "", amount = 0 }`

Thanks @jessta for pointing to `keyValuePairs`. The code is base on this [http://folkertdev.nl/blog/elm-messy-json-value/](http://folkertdev.nl/blog/elm-messy-json-value/).

How can I make this more generic (less `MyRecord`) so I can reuse more code?  
What can be improved, done differently?

```
module Main exposing (main)

import Html exposing (Html, text)
import Json.Decode exposing (..)

(|:) : Decoder (a -> b) -> Decoder a -> Decoder b
(|:) =
    map2 (<|)

serverResponseMy : String
serverResponseMy =
    """
    {
        "url": "https://elm-lang.org",
        "amount": 1.2,
        "somefield": 1
    }
"""

partialDecodeField : Decoder (List ( String, Value ))
partialDecodeField =
    keyValuePairs value

fromResultWithWarning : (String -> b -> Decoder b) -> ( String, Value ) -> b -> b
fromResultWithWarning decoder ( name, value ) record =
    case decodeValue (decoder name record) value of
        Ok newRecord ->
            newRecord

        Err error ->
            Debug.log ("[Warning]: " ++ error) record

feedNameL : (String -> MyRecord -> Decoder MyRecord) -> List ( String, Value ) -> MyRecord
feedNameL decoder elements =
    List.foldl (fromResultWithWarning decoder) defaultMyRecord elements

decodeList : Decoder MyRecord
decodeList =
    map (feedNameL decodeField) partialDecodeField

decodeField : String -> MyRecord -> Decoder MyRecord
decodeField name record =
    case name of
        "url" ->
            succeed (\url -> { record | url = url }) |: string

        "amount" ->
            succeed (\amount -> { record | amount = amount }) |: float

        _ ->
            fail <| "Unhandled field " ++ name

type alias MyRecord =
    { url : String
    , amount : Float
    }

defaultMyRecord : MyRecord
defaultMyRecord =
    MyRecord "" 0

main : Html msg
main =
    text <| toString <| decodeString decodeList serverResponseMy
```

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [April 16, 2018, 3:01pm UTC](https://discourse.elm-lang.org/t/decode-json-object-with-warning-for-unhandled-keys/1133/2 "2018-04-16T15:01:46Z")

</div>

I have a feeling there is a library package for this already, as I remember asking about it before. I’ll see if I can dig back on the old elm-discuss list and find it.

This one, I think:

[http://package.elm-lang.org/packages/zwilias/json-decode-exploration/latest](http://package.elm-lang.org/packages/zwilias/json-decode-exploration/latest)

[https://groups.google.com/forum/#!searchin/elm-discuss/json$20missing$20fields|sort:date/elm-discuss/-WJRY3r8TDs/R57qsuKCAwAJ](https://groups.google.com/forum/#!searchin/elm-discuss/json%2420missing%2420fields%7Csort:date/elm-discuss/-WJRY3r8TDs/R57qsuKCAwAJ)

---

<div class="post-metadata">

**Author:** ![rofrol](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rofrol/32/60_2.png) [@rofrol](https://discourse.elm-lang.org/u/rofrol)\
**Post date:** [April 16, 2018, 8:17pm UTC](https://discourse.elm-lang.org/t/decode-json-object-with-warning-for-unhandled-keys/1133/3 "2018-04-16T20:17:32Z")

</div>

Thanks, though I know about this library and strict mode. I wanted to do this with standard library as this is what you have to deal with in examples, libraries etc.

---

<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:** [April 26, 2018, 8:19pm UTC](https://discourse.elm-lang.org/t/decode-json-object-with-warning-for-unhandled-keys/1133/4 "2018-04-26T20:19:48Z")

</div>

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