How to parse JSON in Elixir Posted in Polyglotbits on 06 of Sep, 2016

Parsing JSON in elixir requires an external tool - and you’ll need to define the types the data needs to be parsed into.

First we will create a new project:

$ mkdir elixir_parse_json
$ cd !$
$ mix new .
$ git init
$ git add .
$ git commit -am "initial import"

Next we will add the poison module. Open up mix.exs and add the two-tupple {:poison, "~> 2.0"} to the deps list. And run the mix deps.get command to get the dependency installed.

Now we’ll need to define the struct to place the data into.

defmodule Person do
  @derive [Poison.Encoder]
  defstruct [:name]

And finally we just need some json to decode - let’s use the same as for the php example.

Poison.decode!(~s({"name": "Claus Witt"}), as: %Person{})

Which should output %Person{name: "Claus Witt"}

More about Elixir