# The inconsistency API design in JSON decoder ...?

**URL:** <https://discourse.elm-lang.org/t/the-inconsistency-api-design-in-json-decoder/8491>\
**Category:** Learn\
**Created:** [July 6, 2022, 7:10pm UTC](https://discourse.elm-lang.org/t/the-inconsistency-api-design-in-json-decoder/8491 "2022-07-06T19:10:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![howtomakeaturn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/howtomakeaturn/32/4620_2.png) [@howtomakeaturn](https://discourse.elm-lang.org/u/howtomakeaturn)\
**Post date:** [July 6, 2022, 7:10pm UTC](https://discourse.elm-lang.org/t/the-inconsistency-api-design-in-json-decoder/8491/1 "2022-07-06T19:10:36Z")

</div>

I’m new to Elm and learning it! It’s very cool!

I have a question and I want to have some feedback, thanks!

Take the example from here: [How to decode complex JSON with Elm? // Tech @ ChefClub //](https://tech.chefclub.tv/en/how-to-decode-complex-json-with-elm)

```auto
type alias Record =
    { uid : String
    , age : Maybe Int
    , version : Float
    }

customDecoder : JD.Decoder Record
customDecoder =
    decode Record
        |> required "uid" JD.string
        |> optional "age" (JD.maybe JD.int) Nothing
        |> hardcoded 1.0

```

I’m wondering why field names sometimes appear in the decoder, and sometimes don’t?

If the field name is good for readability, why the usage here is not

```auto
customDecoder =
    decode Record
        |> required "uid" JD.string
        |> optional "age" (JD.maybe JD.int) Nothing
        |> hardcoded "version" 1.0

```

If the parameter order is enough, then why the usage here is not

```auto
customDecoder =
    decode Record
        |> required JD.string
        |> optional (JD.maybe JD.int) Nothing
        |> hardcoded 1.0

```

I’m wondering if there’s an inconsistency API design issue in JSON decoder here …?

Thanks for any feedback!

---

<div class="post-metadata">

**Author:** ![dta](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dta/32/1917_2.png) [@dta](https://discourse.elm-lang.org/u/dta)\
**Post date:** [July 6, 2022, 7:27pm UTC](https://discourse.elm-lang.org/t/the-inconsistency-api-design-in-json-decoder/8491/2 "2022-07-06T19:27:23Z")

</div>

The field names match up with the names of the JSON object properties.

The positions match up with the order of the fields in the `Record` record.

If you want to read something out of the JSON, you do need the name. What `hardcoded` does is ignore the JSON object and just put the provided value in the next field of the record.

---

<div class="post-metadata">

**Author:** ![lue-bird](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lue-bird/32/4678_2.png) [@lue-bird](https://discourse.elm-lang.org/u/lue-bird)\
**Post date:** [July 6, 2022, 7:47pm UTC](https://discourse.elm-lang.org/t/the-inconsistency-api-design-in-json-decoder/8491/3 "2022-07-06T19:47:34Z")

</div>

To make the above more obvious: You could write your whole `Decoder` like this:

```elm
customDecoder : JD.Decoder Record
customDecoder =
    decode (\uuid age -> { uuid = uuid, age = age, version = 1.0 })
        |> required "uid" JD.string
        |> optional "age" (JD.maybe JD.int) Nothing

```

I [generally recommend this approach](https://dark.elm.dmy.fr/packages/lue-bird/elm-no-record-type-alias-constructor-function/latest/#why)

---

<div class="post-metadata">

**Author:** ![howtomakeaturn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/howtomakeaturn/32/4620_2.png) [@howtomakeaturn](https://discourse.elm-lang.org/u/howtomakeaturn)\
**Post date:** [July 7, 2022, 1:40am UTC](https://discourse.elm-lang.org/t/the-inconsistency-api-design-in-json-decoder/8491/4 "2022-07-07T01:40:21Z")

</div>

Got it! Thank you very much!

---

<div class="post-metadata">

**Author:** ![howtomakeaturn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/howtomakeaturn/32/4620_2.png) [@howtomakeaturn](https://discourse.elm-lang.org/u/howtomakeaturn)\
**Post date:** [July 7, 2022, 1:41am UTC](https://discourse.elm-lang.org/t/the-inconsistency-api-design-in-json-decoder/8491/5 "2022-07-07T01:41:18Z")

</div>

Thanks for the example & link!  
It helps me a lot!  
Will learn more with them! Thanks!

---

<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:** [July 17, 2022, 1:41am UTC](https://discourse.elm-lang.org/t/the-inconsistency-api-design-in-json-decoder/8491/6 "2022-07-17T01:41:36Z")

</div>

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