JSON Encoding a Map in Elixir Using Poison

I am trying to parse my map into json string, how would I do this with poison?

I have tried the following.

iex(19)> test = %{"api_key" => "sklfjklasfj"} %{"api_key" => "sklfjklasfj"} iex(20)> Poison.Encoder.encode(test, []) [123, [[34, ["api_key"], 34], 58, [34, ["sklfjklasfj"], 34]], 125] 

I would expect that

 "{"api_key": "sklfjklasfj"}" 
+8
json elixir
source share
1 answer

I realized that the poison was returning a char_list, which could be sent to a string like this.

 iex(27)> to_string Poison.Encoder.encode(test, []) "{\"api_key\":\"sklfjklasfj\"}" 

As of October 2017 (Poison v3) the code will be

 iex(27)> to_string Poison.encode_to_iodata!(test, []) "{\"api_key\":\"sklfjklasfj\"}" 

or simply

 iex(27)> Poison.encode!(test, []) "{\"api_key\":\"sklfjklasfj\"}" 

without calling to_string .

+13
source share

All Articles