jeromedoyle

jeromedoyle

JSON API response to Structs using Ecto Schema

Does anyone have an example of using Ecto Schemas for parsing JSON api responses?
I’m curious how you can convert this

{
    "id": 283230,
    "fname": "John",
    "lname": "Smith",
    "email": "john.smith@example.com",
    "createdOn": "2016-03-31T10:49:11.386-05:00",
    "updatedOn": "2016-10-31T16:36:12.968-05:00",
    "_embedded": {
      "addresses": [
        {
          "city": "Minneapolis",
          "state": "MN"
        },
        {
          "city": "New Orleans",
          "state": "LA"
        }
      ]
    }
}

into this

%User{
  id: 283230,
  fname: "John",
  lname: "Smith",
  email: "john.smith@example.com",
  createdOn: "2016-03-31T10:49:11.386-05:00",
  updatedOn: "2016-10-31T16:36:12.968-05:00",
  addresses: [
    %Address{
      "city": "Minneapolis",
      "state": "MN"
    },
    %Address{
      "city": "New Orleans",
      "state": "LA"
    }
  ]
}

Most Liked

IvanR

IvanR

One alternative valid way is to use Jason and plain structs with @type t :: ... definitions plus Domo for validation.

Considering MapShaper module from this example app, the structs can be defined like:

defmodule Address do
  @moduledoc false

  use Domo

  defstruct [city: "", state: ""]

  @type t :: %__MODULE__{city: String.t(), state: String.t()}
end

defmodule User do
  @moduledoc false

  use Domo

  today = NaiveDateTime.utc_now()
  defstruct [id: 0, fname: "", lname: "", email: "", createdOn: today, updatedOn: today, addresses: [%Address{}]]

  @type t :: %__MODULE__{
    id: non_neg_integer(),
    fname: String.t(),
    lname: String.t(),
    email: String.t(),
    createdOn: NaiveDateTime.t(),
    updatedOn: NaiveDateTime.t(),
    addresses: [Address.t()]
  }

  defimpl MapShaper.Target do
    def translate_source_map(_value, map) do
      addresses = get_in(map, ["_embedded", "addresses"])
      created_on = map |> get_in(["createdOn"]) |> parse_date()
      updated_on = map |> get_in(["updatedOn"]) |> parse_date()

      map
      |> Map.put("addresses", addresses)
      |> Map.put("createdOn", created_on)
      |> Map.put("updatedOn", updated_on)
    end

    defp parse_date(str) do
      str
      |> NaiveDateTime.from_iso8601()
      |> then(fn
        {:ok, date_time} -> date_time
        _ -> nil
      end)
    end
  end
end

And parsing + validation that looks like:

with {:ok, binary} <- File.read("user.json"),
      {:ok, map} <- Jason.decode(binary),
      user = MapShaper.from_map(%User{}, map),
      {:ok, user} <- User.ensure_type_ok(user) do
  user
end

Returns the following nested struct for the valid input:

%User{
  id: 283230,
  fname: "John",
  lname: "Smith",
  email: "john.smith@example.com",
  createdOn: ~N[2016-03-31 10:49:11.386],
  updatedOn: ~N[2016-10-31 16:36:12.968],
  addresses: [
    %Address{city: "Minneapolis", state: "MN"},
    %Address{city: "New Orleans", state: "LA"}
  ]
}

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement