Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Elixir

Idiom #91 Load JSON file into object

Read from the file data.json and write its content into the object x.
Assume the JSON data is suitable for the type of x.

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

x = JsonTest.get_json("data.json")

Ensure {:poison, "~> version"} entered under deps in mix.exs
(require '[clojure.java.io :refer [file]])
(require '[jsonista.core :refer [read-value]])
(def x (read-value (file "data.json")))

New implementation...