# Custom jason encoder

**URL:** <https://forum.elixirforum.com/t/custom-jason-encoder/16302>\
**Category:** Questions\
**Tags:** jason\
**Created:** [September 5, 2018, 5:36am UTC](https://forum.elixirforum.com/t/custom-jason-encoder/16302 "2018-09-05T05:36:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![script](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/script/32/8725_2.png) [@script](https://forum.elixirforum.com/u/script)\
**Post date:** [September 5, 2018, 5:36am UTC](https://forum.elixirforum.com/t/custom-jason-encoder/16302/1 "2018-09-05T05:36:25Z")

</div>

In the docs its mentioned that we can implement the custom data types with jason instead of `for: Any`. But what if i wanted to write it for some custom attribute like `for: [Ecto.Association.NotLoaded]` .how can I make it work. This is the code

```
      defimpl Jason.Encoder, for: [Ecto.Association.NotLoaded] do
       def encode(%{ __struct__ : _} = struct, _options) do
        struct
        |> Map.from_struct()
        |> sanitize()
        |> Jason.encode!()
       end

     defp sanitize(map) do
    map
   |> Map.delete(: __meta__ )
   |> Map.delete(: __struct__ )
   |> Enum.map(fn
    {k, %Ecto.Association.NotLoaded{}} -> {k, "Not Loaded"}
    other -> other
  end)
  |> Enum.into(%{})
end
end

```

Its working fine `for: Any`. But throws an error if I change it `Ecto.Association.NotLoaded`.

```
       This protocol is implemented for: Any, Atom, BitString, Date, DateTime, Decimal, 
         Ecto.Association.NotLoaded

```

It means it implemented for `Ecto.Association.NotLoaded`. But how can i include it in the modules to see if it works or not. Or Do I have tell my module to use this encoder?

Thanks

---

<div class="post-metadata">

**Author:** ![idi527](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@idi527](https://forum.elixirforum.com/u/idi527)\
**Post date:** [September 5, 2018, 5:42am UTC](https://forum.elixirforum.com/t/custom-jason-encoder/16302/2 "2018-09-05T05:42:58Z")

</div>

👋

Try replacing

```elixir
defimpl Jason.Encoder, for: [Ecto.Association.NotLoaded] do

```

with

```elixir
defimpl Jason.Encoder, for: Ecto.Association.NotLoaded do

```

and

```elixir
Jason.encode!()

```

with

```elixir
Jason.Encode.map()

```

---

<div class="post-metadata">

**Author:** ![script](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/script/32/8725_2.png) [@script](https://forum.elixirforum.com/u/script)\
**Post date:** [September 5, 2018, 6:06am UTC](https://forum.elixirforum.com/t/custom-jason-encoder/16302/3 "2018-09-05T06:06:39Z")

</div>

same error. Works fine for: Any. But throws error for this

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [September 5, 2018, 6:08am UTC](https://forum.elixirforum.com/t/custom-jason-encoder/16302/4 "2018-09-05T06:08:23Z")

</div>

Can you please show a full error and the input you wanted to encode? My assumption is, that you are trying to encode a changeset, and not the actual struct.

---

<div class="post-metadata">

**Author:** ![script](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/script/32/8725_2.png) [@script](https://forum.elixirforum.com/u/script)\
**Post date:** [September 5, 2018, 6:38am UTC](https://forum.elixirforum.com/t/custom-jason-encoder/16302/5 "2018-09-05T06:38:05Z")

</div>

it seems like I have to add `@derive` to few more models to make the code work.  
But how do i check if my custom encoder is working?
