Search⌘ K
AI Features

Transformation: Convert Response

Explore how to convert JSON responses from APIs into Elixir data structures using the Poison library. Understand how to handle success and error responses, configure application environment variables, and improve project organization. This lesson provides practical code examples for decoding GitHub API responses and managing project settings in Elixir.

Convert the response

We’ll need a JSON library to convert the response into a data structure. Searching hex.pm, we found the poison library (no relation to HTTPoison), so let’s add its dependency to our mix.exs file.

defp deps do 
  [
    { :httpoison, "~> 1.0.0" },
    { :poison, "~> 3.1" }, 
  ]
end

If we run mix deps.get, we’ll end up with poison installed.

Next, we convert the body from a string. When we return the message from the GitHub API in gitHub_issues.ex file, we call the Poison.Parser.parse! function as seen below:

def handle_response({ _,
...