Issues with encoding into JSON

Whenever I encode something into JSON, like a map (see below), I get the following issue.

Poison.encode!(%{
  "2013-02-10 00:00:00 -0800": 11,
  "2013-02-11 00:00:00 -0800": 6,
  "2013-02-12 00:00:00 -0800": 3,
  "2013-02-13 00:00:00 -0800": 2,
  "2013-02-14 00:00:00 -0800": 5,
  "2013-02-15 00:00:00 -0800": 3,
  "2013-02-16 00:00:00 -0800": 8,
  "2013-02-17 00:00:00 -0800": 6,
  "2013-02-18 00:00:00 -0800": 6,
  "2013-02-19 00:00:00 -0800": 12,
  "2013-02-20 00:00:00 -0800": 5,
  "2013-02-21 00:00:00 -0800": 5,
  "2013-02-22 00:00:00 -0800": 3,
  "2013-02-23 00:00:00 -0800": 1,
  "2013-02-24 00:00:00 -0800": 10,
  "2013-02-25 00:00:00 -0800": 1,
  "2013-02-26 00:00:00 -0800": 3,
  "2013-02-27 00:00:00 -0800": 2,
  "2013-02-28 00:00:00 -0800": 3,
  "2013-03-01 00:00:00 -0800": 2,
  "2013-03-02 00:00:00 -0800": 8
})

The error I get:

protocol String.Chars not implemented for {:safe, "<div id=\"9cc16e3a-3435-4d08-a4ec-ec10d10cb1a1\" style=\"height: 300px; text-align: center; color: #999; line-height: 300px; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;\">Loading...</div>\n<script type=\"text/javascript\">new Chartkick.LineChart('9cc16e3a-3435-4d08-a4ec-ec10d10cb1a1', {\"2013-03-02 00:00:00 -0800\":8,\"2013-03-01 00:00:00 -0800\":2,\"2013-02-28 00:00:00 -0800\":3,\"2013-02-27 00:00:00 -0800\":2,\"2013-02-26 00:00:00 -0800\":3,\"2013-02-25 00:00:00 -0800\":1,\"2013-02-24 00:00:00 -0800\":10,\"2013-02-23 00:00:00 -0800\":1,\"2013-02-22 00:00:00 -0800\":3,\"2013-02-21 00:00:00 -0800\":5,\"2013-02-20 00:00:00 -0800\":5,\"2013-02-19 00:00:00 -0800\":12,\"2013-02-18 00:00:00 -0800\":6,\"2013-02-17 00:00:00 -0800\":6,\"2013-02-16 00:00:00 -0800\":8,\"2013-02-15 00:00:00 -0800\":3,\"2013-02-14 00:00:00 -0800\":5,\"2013-02-13 00:00:00 -0800\":2,\"2013-02-12 00:00:00 -0800\":3,\"2013-02-11 00:00:00 -0800\":6,\"2013-02-10 00:00:00 -0800\":11}, {});</script>\n"} of type Tuple. This protocol is implemented for the following type(s): Postgrex.Copy, Postgrex.Query, Decimal, Float, DateTime, Time, List, Version.Requirement, Atom, Integer, Version, Date, BitString, NaiveDateTime, URI

Your map is being encoded into json just fine. The error you are seeing comes from some other part of your app. You have a a safe tuple that is part of a Phoenix HTML io_list. Can you show the code where the error actually occurs?

2 Likes

Hi @tio407 please always provide the full stack trace when posting about an error, this will help us direct you to the proper place in the code to investigate.

1 Like

I copied this code https://github.com/buren/chartkick-phoenix-example/blob/master/web/templates/page/index.html.eex

but do not get the same results. This is my error.

    ** (Protocol.UndefinedError) protocol String.Chars not implemented for {:safe, "<div id=\"9cc16e3a-3435-4d08-a4ec-ec10d10cb1a1\" style=\"height: 300px; text-align: center; color: #999; line-height: 300px; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;\">Loading...</div>\n<script type=\"text/javascript\">new Chartkick.LineChart('9cc16e3a-3435-4d08-a4ec-ec10d10cb1a1', {\"2013-03-02 00:00:00 -0800\":8,\"2013-03-01 00:00:00 -0800\":2,\"2013-02-28 00:00:00 -0800\":3,\"2013-02-27 00:00:00 -0800\":2,\"2013-02-26 00:00:00 -0800\":3,\"2013-02-25 00:00:00 -0800\":1,\"2013-02-24 00:00:00 -0800\":10,\"2013-02-23 00:00:00 -0800\":1,\"2013-02-22 00:00:00 -0800\":3,\"2013-02-21 00:00:00 -0800\":5,\"2013-02-20 00:00:00 -0800\":5,\"2013-02-19 00:00:00 -0800\":12,\"2013-02-18 00:00:00 -0800\":6,\"2013-02-17 00:00:00 -0800\":6,\"2013-02-16 00:00:00 -0800\":8,\"2013-02-15 00:00:00 -0800\":3,\"2013-02-14 00:00:00 -0800\":5,\"2013-02-13 00:00:00 -0800\":2,\"2013-02-12 00:00:00 -0800\":3,\"2013-02-11 00:00:00 -0800\":6,\"2013-02-10 00:00:00 -0800\":11}, {});</script>\n"} of type Tuple. This protocol is implemented for the following type(s): Postgrex.Copy, Postgrex.Query, Decimal, Float, DateTime, Time, List, Version.Requirement, Atom, Integer, Version, Date, BitString, NaiveDateTime, URI
        (elixir) lib/string/chars.ex:3: String.Chars.impl_for!/1
        (elixir) lib/string/chars.ex:22: String.Chars.to_string/1
        (helper) lib/helper_web/templates/page/index.html.eex:1: HelperWeb.PageView."index.html"/1
        (helper) lib/helper_web/templates/layout/app.html.eex:26: HelperWeb.LayoutView."app.html"/1```
1 Like

Can you show your page code in full?

1 Like

Linked to it. It looks like it errors out in index.html. I just installed the library and copied the code above to take it for a spin. (This was after trying with my own code and running into the same issue).