# Insert String, Integer, Map, List in a jsonb PostgreSQL column

**URL:** <https://forum.elixirforum.com/t/insert-string-integer-map-list-in-a-jsonb-postgresql-column/19276>\
**Category:** Questions\
**Tags:** ecto, postgres\
**Created:** [January 10, 2019, 2:35pm UTC](https://forum.elixirforum.com/t/insert-string-integer-map-list-in-a-jsonb-postgresql-column/19276 "2019-01-10T14:35:06Z")\
**Posts on this page:** 1\
**Showing post:** 15

<div class="post-metadata">

**Author:** ![Eiji](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/32/36743_2.png) [@Eiji](https://forum.elixirforum.com/u/Eiji)\
**Post date:** [January 11, 2019, 5:40pm UTC](https://forum.elixirforum.com/t/insert-string-integer-map-list-in-a-jsonb-postgresql-column/19276/15 "2019-01-11T17:40:32Z")

</div>

@mischov: I think that this should work, but I did not fully tested it.

```elixir
defmodule Type.Json do
  alias Ecto.Type

  @behaviour Ecto.Type
  @moduledoc """
  To store values that might be any json value.

  Not intended as a general replacement for :map.
  """

  def type, do: :json

  def cast(value) when is_nil(value), do: {:ok, value}
  def cast(value) when is_boolean(value), do: {:ok, value}
  def cast(value) when is_binary(value), do: {:ok, value}
  def cast(value) when is_number(value), do: {:ok, value}
  def cast(value) when is_list(value), do: Type.cast({:array, __MODULE__ }, value)
  def cast(value) when is_map(value), do: Type.cast({:map, __MODULE__ }, value)
  def cast(_), do: :error

  def load(data), do: {:ok, data}

  def dump(value) when is_nil(value), do: {:ok, value}
  def dump(value) when is_boolean(value), do: {:ok, value}
  def dump(value) when is_binary(value), do: {:ok, value}
  def dump(value) when is_number(value), do: {:ok, value}
  def dump(value) when is_list(value), do: Type.dump({:array, __MODULE__ }, value)
  def dump(value) when is_map(value), do: Type.dump({:map, __MODULE__ }, value)
  def dump(_), do: :error
end

```

---

_[View the full topic](https://forum.elixirforum.com/t/insert-string-integer-map-list-in-a-jsonb-postgresql-column/19276)._
