# Conditional JSON decoder with optional fields

**URL:** https://discourse.elm-lang.org/t/conditional-json-decoder-with-optional-fields/7938
**Category:** Learn
**Created:** [November 24, 2021, 1:57pm UTC](https://discourse.elm-lang.org/t/conditional-json-decoder-with-optional-fields/7938 "2021-11-24T13:57:22Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![ben-t](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ben-t/32/3249_2.png) [@ben-t](https://discourse.elm-lang.org/u/ben-t)
#### Post date: [November 24, 2021, 4:22pm UTC](https://discourse.elm-lang.org/t/conditional-json-decoder-with-optional-fields/7938/3 "2021-11-24T16:22:41Z")

</div>

Thanks @wondible

I might be misunderstanding the behaviour of `at`. With this solution, wouldn’t the decoder returned by `decodeRecordTypeDetails` operate on the actual name of the type?

So although the types line up, I don’t think this would work because we need to apply that decoder to a field in the original outer JSON.

I tried something which I think was similar and got the following result:

```auto
Got bad body (Problem with the value at json[0].type:

    {
        "id": 35,
        "name": "Chair"
    }

Expecting an OBJECT with a field named `type`) when attempting to load JSON!

```

Have I misunderstood?

---

_[View the full topic](https://discourse.elm-lang.org/t/conditional-json-decoder-with-optional-fields/7938)._
