I am trying below code
data = Poison.decode!(payload)
|> ProperCase.to_snake_case
Logger.info("data is #{data}")
Where the payload is from Messging Queue
{
"name":"Joe",
"id": "13",
"version": 0
}
With this I am getting an error
[error] Could not process PerfectFlight: %Protocol.UndefinedError{description: "", protocol: String.Chars, value: %{"id" => "13", "name" => "Joe", "version" => 0}}
However, If I change my input json to
"{
\"name\":\"Joe\",
\"id\": \"13\",
\"version\": 0
}"
The Poison.decode()
works pefectly fine. Now the issue is that I don't want to change my input JSON
because of many reasons. What am I missing?
Edit : The code was failing not at the decode but on the next line Logger.info("data is #{data}")
. Since the output of decode
function is not a String
I should rather use IO.inspect as below. Accepting Adams answer for his confidence in decode function.
data = Poison.decode!(payload)
|> ProperCase.to_snake_case
IO.inspect(data)
There is nothing wrong with Poison:
iex(1)> Poison.decode!(~S'{"name":"Joe", "id": "13", "version": 0}')
%{"id" => "13", "name" => "Joe", "version" => 0}
Your example also works for me:
iex(1)> payload = ~S'{"name":"Joe", "id": "13", "version": 0}'
"{\"name\":\"Joe\", \"id\": \"13\", \"version\": 0}"
iex(2)> Poison.decode!(payload) |> ProperCase.to_snake_case
%{"id" => "13", "name" => "Joe", "version" => 0}
The error you are getting is probably because something somewhere is trying to convert a map to a string:
iex(1)> IO.puts %{"id" => "13", "name" => "Joe", "version" => 0}
** (Protocol.UndefinedError) protocol String.Chars not implemented for %{"id" => "13", "name" => "Joe", "version" => 0}
It looks like you're getting an error while trying to debug your problem. You could use IO.inspect
instead of IO.puts
for debugging, and see if you can get a more helpful error message.