tushar

tushar

Decoding Multi Level Json

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 ?

Most Liked

NobbZ

NobbZ

"{\"topic\": \"dummy_topic\", \"msg\": \"{\"fruit\":\"apple\",\"dish\":\"apple-pie\"}\"}"
                                          ^^

That is where the error is, this matches exactly with what I said, the quotes aren’t escaped properly. The following should be valid:

~S'{"topic": "dummy_topic","msg":"{\"fruit\":\"apple\",\"dish\":\"apple-pie\"}"}'

Alternatively, you have to double escape in the msg field:

"{\"topic\": \"dummy_topic\", \"msg\": \"{\\\"fruit\\\":\\\"apple\\\",\\\"dish\\\":\\\"apple-pie\\\"}\"}"

I prefer the sigil approach for readability.

NobbZ

NobbZ

Due to the doublequotes in the value of msg beeing improperly escaped, you don’t have valid JSON.

NobbZ

NobbZ

Jason.encode(%{
  topic: "dummy",
  msg: Jason.encode(%{a: "message blob"})
})

# or

~s'{"topic":"dummy","msg":#{Jason.encode(Jason.encode(%{a: "message blob"}))}}'

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.

Last Post!

NobbZ

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?

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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 records...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement