tushar
Below is the json I get after encoding it using Jason
"{\"topic\": \"dummy_topic\", \"msg\": \"{\"fruit\":\"apple\",\"dish\":\"apple-pie\"}\"}"
I am trying to pass it as the body of the request while calling an API using HTTPoison , it gives me the json decode error
tried the same on terminal and I get below ,
iex(10)> var = "{\"topic\": \"dummy_topic\", \"msg\": \"{\"fruit\":\"apple\",\"dish\":\"apple-pie\"}\"}"
"{\"topic\": \"dummy_topic\", \"msg\": \"{\"fruit\":\"apple\",\"dish\":\"apple-pie\"}\"}"
iex(11)> Jason.decode(var)
{:error,
%Jason.DecodeError{
data: "{\"topic\": \"dummy_topic\", \"msg\": \"{\"fruit\":\"apple\",\"dish\":\"apple-pie\"}\"}",
position: 35,
token: nil
}}
Which is the exact same error I get when when I use HTTPoison ,
my expectation is I should be getting something like below when I use Jason.decode ,
%{
"topic" => "dummy_topic",
"msg" => %{"fruit" => "apple", "dish" => "apple-pie"}
}
Do you know where I am getting it wrong or what can be done to get the expected output ?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
Due to the doublequotes in the value of
msgbeeing improperly escaped, you don’t have valid JSON.ityonemo
It’s in position 35, as the error states.
NobbZ
That is where the error is, this matches exactly with what I said, the quotes aren’t escaped properly. The following should be valid:
Alternatively, you have to double escape in the
msgfield:I prefer the sigil approach for readability.
tushar
Thanks @NobbZ ,
It was happening because of the sigil , I found it finally and solved it by replacing the body of the inner json into a String that would contain a place holder for it.
So my requirement here was the value of a key in the map would be another json , that json would also be derived from a map , I used
Jason.encodeto do this , it puts an escape\"before the starting braces ,and then after composing the body of the final json I used sigil (
~s) which had put another escape prefixing the escape , which was causing a problem duringJason.decodesince it was unable to find the start of the inner map.How I fixed it is in the final json I just put a placeholder
<body>and then replaced the body with theJason.encodeinner json string.With sigils is there a way to avoid escaping the escape characters to construct the right json that can be used flawlessly with
Jason.decodeNobbZ
These variants should both work. I’m not sure about exact quoting here, so it might be, that you need to wrap the interpolated value in another pair of quotes.
I prefer the first approach for readability.
tushar
"{\"topic\": \"dummy_topic\", \"msg\": \"{\"fruit\":\"apple\",\"dish\":\"apple-pie\"}\"}"→ This does not work"{\"topic\": \"dummy_topic\", \"msg\": {\"fruit\":\"apple\",\"dish\":\"apple-pie\"}}"→ This worksThe only difference between the strings is
"{\"topic\": \"dummy_topic\", \"msg\": *\"*{\"fruit\":\"apple\",\"dish\":\"apple-pie\"}*\"*}"The escape sequences causing problem are wrapped in *
Those two escape sequences if removed , makes it work fine.
Is there a way with Sigils I can choose to not have those escapes ? or I have to do string manipulation to have it working ?
NobbZ
I thought you want msg to be a string containing JSON? Then of course you can not remove the wrapping quotes that make it a string.
tushar
True but as is , the string causes a problem when I try to decode it using
Jason.decodeIts unable to find the inner map… , any way we can counter that problem and have my desired structure back from the json string , if you need the original Elixir map which I converted to Json and and want to have it back , let me know.
NobbZ
You can’t decide it, because you encode it wrong, that’s what I am trying to tell you the whole time.
Have you tried my suggestions from Decoding Multi Level Json - #6 by NobbZ?