# miniBill/elm-codec - How to do \`Json.Decode.field "data" decoder\`?

**URL:** <https://discourse.elm-lang.org/t/minibill-elm-codec-how-to-do-json-decode-field-data-decoder/4268>\
**Category:** Learn\
**Created:** [September 5, 2019, 11:54pm UTC](https://discourse.elm-lang.org/t/minibill-elm-codec-how-to-do-json-decode-field-data-decoder/4268 "2019-09-05T23:54:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![derrickbeining](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/derrickbeining/32/5078_2.png) [@derrickbeining](https://discourse.elm-lang.org/u/derrickbeining)\
**Post date:** [September 5, 2019, 11:54pm UTC](https://discourse.elm-lang.org/t/minibill-elm-codec-how-to-do-json-decode-field-data-decoder/4268/1 "2019-09-05T23:54:00Z")

</div>

Really enjoying using [miniBill/elm-codec](https://package.elm-lang.org/packages/miniBill/elm-codec/latest/Codec), but I’m curious if there’s an equivalent way of doing this:

```elm
decoder =
    Json.Decode.field "data" decoder

```

Basically, I have this one-off scenario of an http result that comes back on `"data"` that I need to decode my domain-specific value off of. So far, the best I’ve come up with is this:

```elm
decoder_ =
    Codec.object identity
        |> Codec.field "data" identity (Codec.list codec)
        |> Codec.buildObject
        |> Codec.decoder

```

Really, the only part that feels weird is the use of `identity` and the fact that it’s a good bit more verbose that the `Json.Decode` version. Anyone have a better way using `elm-codec`? @miniBill, your input would be much appreciated if you happen to see this. Happy to hear from anyone else too 🙂

---

<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:** [September 6, 2019, 9:07am UTC](https://discourse.elm-lang.org/t/minibill-elm-codec-how-to-do-json-decode-field-data-decoder/4268/2 "2019-09-06T09:07:09Z")

</div>

> [@derrickbeining](#):
>
> ```
> decoder_ = 
> Codec.object identity
> |> Codec.field "data" identity (Codec.list codec)
> |> Codec.buildObject
> |> Codec.decoder
> 
> ```

I suppose you could just make a helper function for it? Something like:

```
fieldCodec : String -> Codec a -> Codec a
fieldCodec name codec = 
    Codec.object identity
       |> Codec.field name identity codec
       |> Codec.buildObject

```

---

<div class="post-metadata">

**Author:** ![miniBill](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/minibill/32/800_2.png) [@miniBill](https://discourse.elm-lang.org/u/miniBill)\
**Post date:** [September 6, 2019, 12:03pm UTC](https://discourse.elm-lang.org/t/minibill-elm-codec-how-to-do-json-decode-field-data-decoder/4268/3 "2019-09-06T12:03:42Z")

</div>

I’d just use Json.Decode directly. `elm-codec` is useful to guarantee that encoders and decoders are symmetric and to have a nice-ish api for custom types.  
In this case… it gets you no advantage.

Or you could do what @rupert suggested of course!

---

<div class="post-metadata">

**Author:** ![derrickbeining](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/derrickbeining/32/5078_2.png) [@derrickbeining](https://discourse.elm-lang.org/u/derrickbeining)\
**Post date:** [September 6, 2019, 12:10pm UTC](https://discourse.elm-lang.org/t/minibill-elm-codec-how-to-do-json-decode-field-data-decoder/4268/4 "2019-09-06T12:10:43Z")

</div>

I see. Makes sense. Just wanted to make I wasn’t overlooking something. Thanks for taking a look!

---

<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:** [September 6, 2019, 12:48pm UTC](https://discourse.elm-lang.org/t/minibill-elm-codec-how-to-do-json-decode-field-data-decoder/4268/5 "2019-09-06T12:48:08Z")

</div>

Could `miniBill/elm-codec` expose a way of building one directly from a `Decoder` and `Encoder`. Something like

```
codec : Decoder a -> Encoder a -> Codec a

```

For situations where you want to do something a bit unusual, but still be able to create a Codec, in order to be able to combine it with other Codecs.

The downside is that it becomes possible to accidentally create a Codec that is not symmetrical; perhaps ok if it is explained clearly in the docs for this constructor - the caller takes on a small risk in order to do something more unusual.

Another advantage would be that it makes `miniBill/elm-codec` extensible. If I write my own symmetric encoder/decoder pair, I can hook it in.

I would want to do this when there are several choices in how to encode custom types into JSON, and I am forced to use a different scheme to what `elm-codec` uses because the JSON data model I have does it a different way.

---

<div class="post-metadata">

**Author:** ![miniBill](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/minibill/32/800_2.png) [@miniBill](https://discourse.elm-lang.org/u/miniBill)\
**Post date:** [September 7, 2019, 2:50pm UTC](https://discourse.elm-lang.org/t/minibill-elm-codec-how-to-do-json-decode-field-data-decoder/4268/6 "2019-09-07T14:50:21Z")

</div>

I’ve resisted adding a `codec` function because in my original plan `elm-codec` would allow you to also build byte codecs (in the meantime I’ve helped @MartinS craft `MartinSStewart/elm-codec-bytes`, so that’s no longer applicable) or other things. After seeing how people use it, and how I use it myself, I think I can actually expose it. Look forward to a new release!

---

<div class="post-metadata">

**Author:** ![MartinS](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/martins/32/3137_2.png) [@MartinS](https://discourse.elm-lang.org/u/MartinS)\
**Post date:** [September 7, 2019, 3:06pm UTC](https://discourse.elm-lang.org/t/minibill-elm-codec-how-to-do-json-decode-field-data-decoder/4268/7 "2019-09-07T15:06:22Z")

</div>

I guess I’ll add a `codec` function as well to keep the API’s relatively consistent?

---

<div class="post-metadata">

**Author:** ![miniBill](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/minibill/32/800_2.png) [@miniBill](https://discourse.elm-lang.org/u/miniBill)\
**Post date:** [September 7, 2019, 3:25pm UTC](https://discourse.elm-lang.org/t/minibill-elm-codec-how-to-do-json-decode-field-data-decoder/4268/8 "2019-09-07T15:25:43Z")

</div>

`miniBill/elm-codec` 1.2.0 is out now!  
It adds `fail`, `andThen`, `lazy`, `value` and `build`.  
Naming the function `codec` would have been a pain for the implementation.

---

<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:** [September 8, 2019, 11:05am UTC](https://discourse.elm-lang.org/t/minibill-elm-codec-how-to-do-json-decode-field-data-decoder/4268/9 "2019-09-08T11:05:01Z")

</div>

> [@miniBill](#):
>
> Naming the function `codec` would have been a pain for the implementation.

Sure, `build` is perfectly good name for it too. 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:** [September 18, 2019, 11:05am UTC](https://discourse.elm-lang.org/t/minibill-elm-codec-how-to-do-json-decode-field-data-decoder/4268/10 "2019-09-18T11:05:06Z")

</div>

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