# How to decode a json to custom model

**URL:** <https://discourse.elm-lang.org/t/how-to-decode-a-json-to-custom-model/4968>\
**Category:** Learn\
**Created:** [January 10, 2020, 4:43pm UTC](https://discourse.elm-lang.org/t/how-to-decode-a-json-to-custom-model/4968 "2020-01-10T16:43:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gatsu](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/gatsu/32/2854_2.png) [@gatsu](https://discourse.elm-lang.org/u/gatsu)\
**Post date:** [January 10, 2020, 4:43pm UTC](https://discourse.elm-lang.org/t/how-to-decode-a-json-to-custom-model/4968/1 "2020-01-10T16:43:14Z")

</div>

Hi everyone, I’m quite new in Elm, I need to decode a json, using NoRedInk Json Decode pipe.  
In my model I splitted the data that comes from server in several types and when I have to decode I have to do something like:

decoder : Decoder MyType  
decoder =  
Decode.succeed  
(\field1 field2 subfield1 subfield2 field3 field4 -\>  
MyType  
field1 field2  
SubType  
subfield1 subfield2  
field3 field4  
)  
|\> required “field1” string  
|\> required “field2” string  
…  
is there a better way that avoids the using of the anonymous function?

Thanks!

---

<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:** [January 10, 2020, 4:59pm UTC](https://discourse.elm-lang.org/t/how-to-decode-a-json-to-custom-model/4968/2 "2020-01-10T16:59:10Z")

</div>

There are a few ways you could do this.

You could extract the anonymous function to a standalone named function. This just moves the problem somewhere else but it does clean up your decoder:

```elm
decoder : Decoder MyType
decoder =
  Decode.succeed constructMyType
    |> required "field1" string
    |> required "field2" string
    |> required "subfield1" string
    |> required "subfield2" string
    |> required "field3" string
    |> required "field4" string

```

You could also break out a separate decoder for the sub-field to allow you to lean only on the default constructors you get. `Json.Decode.Pipeline.custom` allows you to specify a custom decoder to get data at the current scope in the JSON document rather than nesting under a key the way `required` does.

```elm
decoder : Decoder MyType
decoder =
  Decode.succeed MyType
    |> required "field1" string
    |> required "field2" string
    |> custom subTypeDecoder
    |> required "field3" string
    |> required "field4" string

subTypeDecoder : Decoder SubType
subTypeDecoder =
  Decode.succeed SubType
    |> required "subfield1" string
    |> required "subfield2" string
```

---

<div class="post-metadata">

**Author:** ![gatsu](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/gatsu/32/2854_2.png) [@gatsu](https://discourse.elm-lang.org/u/gatsu)\
**Post date:** [January 13, 2020, 8:58am UTC](https://discourse.elm-lang.org/t/how-to-decode-a-json-to-custom-model/4968/3 "2020-01-13T08:58:42Z")

</div>

Thank you! Very helpful

---

<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:** [January 23, 2020, 8:58am UTC](https://discourse.elm-lang.org/t/how-to-decode-a-json-to-custom-model/4968/4 "2020-01-23T08:58:45Z")

</div>

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