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

Tee
can someone please explain to me how Enum.reduce works with maps
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement