tushar

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 ?

Showing Posts 9 to 1

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?

tushar

tushar OP

True but as is , the string causes a problem when I try to decode it using Jason.decode

Its 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

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

tushar OP

"{\"topic\": \"dummy_topic\", \"msg\": \"{\"fruit\":\"apple\",\"dish\":\"apple-pie\"}\"}" → This does not work

iex(1)> Jason.decode("{\"topic\": \"dummy_topic\", \"msg\": \"{\"fruit\":\"apple\",\"dish\":\"apple-pie\"}\"}")
{:error,
 %Jason.DecodeError{
   data: "{\"topic\": \"dummy_topic\", \"msg\": \"{\"fruit\":\"apple\",\"dish\":\"apple-pie\"}\"}",
   position: 35,
   token: nil
 }}

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

iex(2)> Jason.decode("{\"topic\": \"dummy_topic\", \"msg\": {\"fruit\":\"apple\",\"dish\":\"apple-pie\"}}")
{:ok,
 %{
   "msg" => %{"dish" => "apple-pie", "fruit" => "apple"},
   "topic" => "dummy_topic"
 }}

The 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

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.

tushar

tushar OP

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.encode to 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 during Jason.decode since 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 the Jason.encode inner 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.decode

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.

ityonemo

ityonemo

It’s in position 35, as the error states.

NobbZ

NobbZ

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
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
RemyXRenard
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
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
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
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews