Stingray

Stingray

Problem with decoding backslash in JSON

I’m faced with a problem in websockets when I received a message with backslash. I escapes backslash with double ‘’ but error is raised.

So I decide to check how Poison lib is working in iex console:
Poison.decode! "{\"string\":\"a\\b\"}"
return me:
%{"string" => "a\b"}
and
Poison.decode! "{\"string\":\"a\\c\"}"
return me:
** (Poison.SyntaxError) Unexpected token: c
but I expect
%{"string" => "a\c"}

What wrong with my string?

Marked As Solved

Marcus

Marcus

No, that is the representation of the string with the escaped backslash. If you print the string with IO.puts you get a\b.

Try the sigli ~S in iex.

iex(21)> ~S|{"string":"a\\c"}|                # generates a string with no escaping
"{\"string\":\"a\\\\c\"}"                     # the string with escaping
iex(22)> Poison.decode(~S|{"string":"a\\c"}|) # escaped just for the parser
{:ok, %{"string" => "a\\c"}}
iex(23)> ~S|a\c|
"a\\c"
iex(24)> IO.puts("a\\c")
a\c
:ok

Also Liked

talentdeficit

talentdeficit

Rebar3 Core Team

you have two levels of escaping going on. one for iex and one for json

“{“string”:“a\\b”}” in iex is equivalent to the json string ‘{“string”:“a\b”}’ which is the literal character “a” followed by the escape sequence for a backspace

if you want the literal character “a” followed by the literal character “” followed by the literal character “b” you want the string ‘{“string”:“a\\b”}’ which written in iex/elixir is “{“string”:“a\\\\b”}”

arcanemachine

arcanemachine

I was confused when this worked, even though I had already tried using the ~s sigil without success.

It turns out that the ~S sigil (with a capital ‘S’) doesn’t do any interpolation and ignores escape codes, which solved the problem for me:

https://hexdocs.pm/elixir/sigils.html#interpolation-and-escaping-in-string-sigils

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement