# Cannot convert struct to map with Jason @derive

**URL:** <https://forum.elixirforum.com/t/cannot-convert-struct-to-map-with-jason-derive/48711>\
**Category:** Questions\
**Tags:** ecto, jason\
**Created:** [July 1, 2022, 11:12am UTC](https://forum.elixirforum.com/t/cannot-convert-struct-to-map-with-jason-derive/48711 "2022-07-01T11:12:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zatae](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zatae/32/23640_2.png) [@zatae](https://forum.elixirforum.com/u/zatae)\
**Post date:** [July 1, 2022, 11:12am UTC](https://forum.elixirforum.com/t/cannot-convert-struct-to-map-with-jason-derive/48711/1 "2022-07-01T11:12:56Z")

</div>

I have a class `Company` (`Echo.Schema`)

```elixir
defmodule MyProject.Company do
  use Ecto.Schema

  ...
  
  @derive {Jason.Encoder, only: [:name]}
  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "companies" do
    field(:name, :string)
    field(:count, :integer, virtual: true)
    has_many(:car, Car, foreign_key: :company_id, references: :id)
    timestamps()
  end

  ...
end

```

When I try to convert a `Company` to a `Map`, it triggers this error:

`company |> Map.from_struct()`

```elixir
[error] GenServer #PID<0.820.0> terminating
** (RuntimeError) cannot encode metadata from the : __meta__ field for MyProject.Fleet to JSON. This metadata is used internally by Ecto and should never be exposed externally.

You can either map the schemas to remove the : __meta__ field before encoding to JSON, or explicit list the JSON fields in your schema:

    defmodule MyProject.Fleet do
      # ...

      @derive {Jason.Encoder, only: [:name, :title, ...]}
      schema ... do
...

```

I don’t understand the error since `@derive` is set in the model ? Did I forgot to do something ?

I have set **Jason** as Phoenix JSON parsing lib in `config/config.ex` :

```elixir
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

```

---

<div class="post-metadata">

**Author:** ![adamu](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/adamu/32/31482_2.png) [@adamu](https://forum.elixirforum.com/u/adamu)\
**Post date:** [July 1, 2022, 12:06pm UTC](https://forum.elixirforum.com/t/cannot-convert-struct-to-map-with-jason-derive/48711/2 "2022-07-01T12:06:37Z")

</div>

`Map.from_struct()` is not related to `Jason.Encoder`. `Json.Encoder` is a protocol for converting structs to JSON, not maps.

However, both are working for me in this example:

```elixir
defmodule MyStruct do
  use Ecto.Schema
  @derive {Jason.Encoder, only: [:name]}

  schema "my_struct" do
    field(:name, :string)
    field(:other, :string)
  end
end

```

Usage:

```elixir
iex(1)> Jason.encode(%MyStruct{})
{:ok, "{\"name\":null}"}
iex(2)> Map.from_struct(%MyStruct{})
%{
  __meta__ : #Ecto.Schema.Metadata<:built, "my_struct">,
  id: nil,
  name: nil,
  other: nil
}

```

---

<div class="post-metadata">

**Author:** ![zatae](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zatae/32/23640_2.png) [@zatae](https://forum.elixirforum.com/u/zatae)\
**Post date:** [July 1, 2022, 12:28pm UTC](https://forum.elixirforum.com/t/cannot-convert-struct-to-map-with-jason-derive/48711/3 "2022-07-01T12:28:58Z")

</div>

I redacted too much code and forgot to tell that this was passed to [`push_event()`](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#push_event/3) right after.

```elixir
push_event(
  socket,
  "show-company-detail",
  company |> Map.from_struct()
)}

```

So my guess is that `push_event` is using **Jason** to turn the map into **JSON**. And because I already turned the `Company` into a `Map` before, **Jason** doesn’t clean any fields and ` __meta__ ` is still there, triggering this error.

Thanks for pointing out what I was not understanding correctly!
