Poison: How to decode to structs when the json value could be an object, or a list of objects
<p>I'm trying to decode a the following json strings using Poison </p>

<pre><code>iex(1)&gt; fetch(1)
{:ok,
 "{\"name\":\"Anabela\",\"surname\":\"Neagu\",\"gender\":\"female\",\"region\":\"Romania\"}"}
iex(2)&gt; fetch(2)
{:ok,
 "[{\"name\":\"Juana\",\"surname\":\"Su├írez\",\"gender\":\"female\",\"region\":\"Argentina\"},{\"name\":\"ðíðÁÐÇð│ðÁð╣\",\"surname\":\"ðƒð╗ð¥Ðéð¢ð©ð║ð¥ð▓\",\"gender\":\"male\",\"region\":\"Russia\"}]"}
</code></pre>

<p>Doing a <code>fetch(1) |&gt; decode_response</code> won't work although it is for param strictly sup to 1.</p>

<p>I do have the following error</p>

<pre><code>10:07:52.663 [error] GenServer Newsequence.Server terminating
** (BadMapError) expected a map, got: {"gender", "female"}
    (stdlib) :maps.find("gender", {"gender", "female"})
    (elixir) lib/map.ex:145: Map.get/3
    lib/poison/decoder.ex:49: anonymous fn/3 in Poison.Decode.transform_struct/4
    (stdlib) lists.erl:1262: :lists.foldl/3
    lib/poison/decoder.ex:48: Poison.Decode.transform_struct/4
    lib/poison/decoder.ex:24: anonymous fn/5 in Poison.Decode.transform/4
    (stdlib) lists.erl:1262: :lists.foldl/3
    lib/poison/decoder.ex:24: Poison.Decode.transform/4 Last message: {:getpeople, 1} State: {[], #PID&lt;0.144.0&gt;}
</code></pre>

<p>My function is as below :</p>

<pre><code>def decode_response({:ok, body}) do
    Poison.decode!(body, as: [%Personne{}])
end
</code></pre>

<p>I then think for param equal to 1, function should be </p>

<pre><code>   def decode_response({:ok, body}) do
        Poison.decode!(body, as: %Personne{})
    end
</code></pre>

<p>I finally thought it was a good idea to count tuples within the string given by my fetch function and use a guard to choose which decode_response to use, but i don't know how. </p>

<p>May someone please point me on the right direction ?</p>

<p>Regards,</p>

<p>pierre</p>