# Set default values while decoding JSON

**URL:** <https://discourse.elm-lang.org/t/set-default-values-while-decoding-json/6387>\
**Category:** Learn\
**Created:** [October 9, 2020, 9:06pm UTC](https://discourse.elm-lang.org/t/set-default-values-while-decoding-json/6387 "2020-10-09T21:06:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwtilma](https://avatars.discourse-cdn.com/v4/letter/i/f0a364/32.png) [@iwtilma](https://discourse.elm-lang.org/u/iwtilma)\
**Post date:** [October 9, 2020, 9:06pm UTC](https://discourse.elm-lang.org/t/set-default-values-while-decoding-json/6387/1 "2020-10-09T21:06:46Z")

</div>

I want to decode JSON, but my type alias needs more information than the JSON provides. I use this type alias:

```
type alias Location =
  { id : Int
  , name : String
  , checked : Bool
  }

```

The JSON only contains the fields id and name. The field checked should be True by default. I have no idea where to add this boolean.  
So far I have this:

```
decodeLocations : Decoder (List Location)
decodeLocations = 
  Dec.field "locations" ( Dec.list ( Dec.map2 Location
      ( Dec.field "id" Dec.int )
      ( Dec.field "name" Dec.string )
    ) )

...

type Msg 
  = GotLocations ( Result Http.Error ( List Location ) )

```

This results in the compiler stating a type mismatch:

```
This `field` call produces:

    Decoder (List (Bool -> Location))

But the type annotation on `decodeLocations` says it should be:

    Decoder (List Location)

```

I understand this mismatch, but where could I add the Bool?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Atlewee](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/atlewee/32/4603_2.png) [@Atlewee](https://discourse.elm-lang.org/u/Atlewee)\
**Post date:** [October 9, 2020, 10:35pm UTC](https://discourse.elm-lang.org/t/set-default-values-while-decoding-json/6387/2 "2020-10-09T22:35:12Z")

</div>

```
D.field "locations" <| D.list <|
  D.map3 Location
    ( D.field "id" D.int )
    ( D.field "name" D.string )
    ( D.succeed True )

```

You use the .succeed decoder. It does not evaluate anything, but gives a sucessful decoding of whatever value you pass to it 🙂

---

<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 10, 2020, 12:07am UTC](https://discourse.elm-lang.org/t/set-default-values-while-decoding-json/6387/3 "2020-10-10T00:07:52Z")

</div>

It’s also possible to stick with `map2` by replacing `Location` with a lambda function:

```auto
decodeLocations : Decoder (List Location)
decodeLocations =
    Dec.field "locations"
        (Dec.list
            (Dec.map2 (\id name -> { id = id, name = name, checked = True })
                (Dec.field "id" Dec.int)
                (Dec.field "name" Dec.string)
            )
        )

```

---

<div class="post-metadata">

**Author:** ![iwtilma](https://avatars.discourse-cdn.com/v4/letter/i/f0a364/32.png) [@iwtilma](https://discourse.elm-lang.org/u/iwtilma)\
**Post date:** [October 10, 2020, 7:10am UTC](https://discourse.elm-lang.org/t/set-default-values-while-decoding-json/6387/4 "2020-10-10T07:10:21Z")

</div>

Thank you both. Both approaches help me to discover possibilities I would never think of.  
Thanks Atlewee to use the \<| operator as well, showing me how to use it in this situation.  
I think I will go with the Dec.succeed, because it keeps the name of the type in the function.

Any suggestions on a good beginners tutorial on Json decoding? (I read “An Introduction to Elm” of course.)

---

<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 10, 2020, 9:26am UTC](https://discourse.elm-lang.org/t/set-default-values-while-decoding-json/6387/5 "2020-10-10T09:26:21Z")

</div>

> I think I will go with the Dec.succeed, because it keeps the name of the type in the function

Just for full understanding, you can keep “the name of the type” in the function in the `map2` situation too:

```auto
decodeLocations : Decoder (List Location)
decodeLocations =
    Dec.field "locations"
        (Dec.list
            (Dec.map2 (\id name -> Location id name True)
                (Dec.field "id" Dec.int)
                (Dec.field "name" Dec.string)
            )
        )

```

The `map3` version can also be written that way:

```auto
decodeLocations : Decoder (List Location)
decodeLocations =
    Dec.field "locations"
        (Dec.list
            (Dec.map3 (\id name checked -> Location id name checked)
                (Dec.field "id" Dec.int)
                (Dec.field "name" Dec.string)
                (Dec.succeed True)
            )
        )

```

However, directly calling `Location` like that is usually frowned upon. It’s much more clear to just “build the record instead”: `{ id = id, name = name, checked = checked }`. But it’s good to know that when you make a `type alias Foo` for a record (yes, only for records!), you get not only a _type_ named `Foo`, Elm also auto-generates a _function_ called `Foo` that takes all the stuff in the record in order and builds such a record. It is that auto-generated function that you use in the body of `decodeLocations`, while the type with the same name is used in the `decodeLocations : Decoder (List Location)` type annotation.

Yet one way of doing it is exploiting the fact that the auto-generated function takes the record contents in a specific order. So you _could_ move `checked : Bool` _first_ in the record, then you could do (but I’m not saying it’s a good solution!):

```auto
type alias Location =
  { checked : Bool
  , name : String
  , id : Int
  }

decodeLocations : Decoder (List Location)
decodeLocations =
    Dec.field "locations"
        (Dec.list
            (Dec.map2 (Location True)
                (Dec.field "id" Dec.int)
                (Dec.field "name" Dec.string)
            )
        )

```

You don’t need to understand all of those details right now, but it might be handy some time! Either way, I like @Atlewee’s solution best too.

> Any suggestions on a good beginners tutorial on Json decoding?

This blog post was enlightening for me at least:

> **[Getting Unstuck with Elm JSON Decoders](https://thoughtbot.com/blog/getting-unstuck-with-elm-json-decoders)**
>
> Solving a Maybe problem is often easier than solving a Decode one.

---

<div class="post-metadata">

**Author:** ![iwtilma](https://avatars.discourse-cdn.com/v4/letter/i/f0a364/32.png) [@iwtilma](https://discourse.elm-lang.org/u/iwtilma)\
**Post date:** [October 11, 2020, 11:53am UTC](https://discourse.elm-lang.org/t/set-default-values-while-decoding-json/6387/6 "2020-10-11T11:53:18Z")

</div>

Thanks a lot, lydell, for elaborating on the subject. It is helping me a lot!

---

<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 21, 2020, 11:53am UTC](https://discourse.elm-lang.org/t/set-default-values-while-decoding-json/6387/7 "2020-10-21T11:53:30Z")

</div>

Closed on October 21, 2020, 11:53am UTC

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