Skip to content Skip to sidebar Skip to footer

Encoding Erlang Maps As Json With Strings For Parsing By Javascript?

I'm trying to take an Erlang map like #{'breakfast' => 'leftovers'} and encode as a JSON map. I tried converting a list with jiffy for example (tunnel@127.0.0.1)27> binary_t

Solution 1:

The problem is that in Erlang the string "hello" is just a list of integer. The libraries that encode Erlang maps into JSON interpret strings as JSON lists, that is why you get a list of integers in the output.

In order to get JSON strings you need to use Erlang binaries as the values in your maps:

Food= #{<<"breakfast">>=><<"leftovers">>},
jiffy:encode(Food).
%%=<<"{ \"breakfast\" : \"leftovers\" }">>

jiffy is consistent so it will also decode JSON strings as Erlang binaries, which you need to take into account when using jiffy:decode/1.

Post a Comment for "Encoding Erlang Maps As Json With Strings For Parsing By Javascript?"