The most correct way to read a JSON file

What is most correct way to open, read and parse JSON file with poison?

For example if we have example.json file in root of some project.

Is it?

def get_json(filename) do
    # ....
end

P.S Sorry for question i’m :eggplant: in Elixir

2 Likes

I’m not sure if it’s the correct way, but I would do something like this

defmodule Test do
  def get_json(filename) do
    with {:ok, body} <- File.read(filename),
         {:ok, json} <- Poison.decode(body), do: {:ok, json}
  end
end
20 Likes

Me too, but looks nice. Thank you!

1 Like