Elixir provides(through erlang) some functions which can reflect upon the data-structures to check their type like is_map/1, is_list/1, is_number/1, is_boolean/1, is_binary/1, is_nil/1
etc. From docs
Try to go the common data-types you will have in your response. They could be a primitive, map or list of primitives where primitives are like boolean, numeric or a string.
Write a function which tries to recursively go through the data-structure you get until it reaches a primitive and then return the stringifed primitive
Ex. for maps, go through every value(ignore key) and if the value is not a primitive, call the function recursively with that node until you reach a primitive and can return a stringified value. Similar for lists
Something like this should work:
defmodule Test do
def stringify(data) do
cond do
# <> is concatenation operator
# -- call stringify() for every value in map and return concatenated string
is_map(data) -> data |> Map.values |> Enum.reduce("", fn x, acc -> acc <> stringify(x) end)
# -- call stringify() for every element in list and return concatenated string
is_list(data) -> data |> Enum.reduce("", fn x, acc -> acc <> stringify(x) end)
is_boolean(data) -> to_string(data)
is_number(data) -> to_string(data)
is_binary(data) -> data # elixir strings are binaries
true -> ""
end
end
end
# For testing...
{:ok, %HTTPoison.Response{body: sbody}} = HTTPoison.get("https://pokemon.fandom.com/api/v1/Articles/AsSimpleJson?id=2409")
{:ok, body} = Poison.decode(sbody)
Test.stringify(body)
# returns
"Cyndaquil (Japanese: ヒノアラシ Hinoarashi) is the Fire-type Starter..."