# Decoding Multi Level Json

**URL:** <https://forum.elixirforum.com/t/decoding-multi-level-json/32925>\
**Category:** Questions\
**Tags:** json, jason, elixir-httpoison, multi-level-json, decode\
**Created:** [July 8, 2020, 5:56pm UTC](https://forum.elixirforum.com/t/decoding-multi-level-json/32925 "2020-07-08T17:56:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tushar](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@tushar](https://forum.elixirforum.com/u/tushar)\
**Post date:** [July 8, 2020, 5:56pm UTC](https://forum.elixirforum.com/t/decoding-multi-level-json/32925/1 "2020-07-08T17:56:25Z")

</div>

Below is the json I get after encoding it using Jason

```elixir
"{\"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 ,

```elixir
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 ,

```elixir
%{
  "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 ?

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [July 8, 2020, 6:00pm UTC](https://forum.elixirforum.com/t/decoding-multi-level-json/32925/2 "2020-07-08T18:00:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ityonemo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/32/11341_2.png) [@ityonemo](https://forum.elixirforum.com/u/ityonemo)\
**Post date:** [July 9, 2020, 4:04am UTC](https://forum.elixirforum.com/t/decoding-multi-level-json/32925/3 "2020-07-09T04:04:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [July 9, 2020, 5:53am UTC](https://forum.elixirforum.com/t/decoding-multi-level-json/32925/4 "2020-07-09T05:53:10Z")

</div>

```elixir
"{\"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:

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

```

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

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

```

I prefer the sigil approach for readability.

---

<div class="post-metadata">

**Author:** ![tushar](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@tushar](https://forum.elixirforum.com/u/tushar)\
**Post date:** [July 9, 2020, 6:21am UTC](https://forum.elixirforum.com/t/decoding-multi-level-json/32925/5 "2020-07-09T06:21:06Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [July 9, 2020, 6:26am UTC](https://forum.elixirforum.com/t/decoding-multi-level-json/32925/6 "2020-07-09T06:26:32Z")

</div>

```ex
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.

---

<div class="post-metadata">

**Author:** ![tushar](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@tushar](https://forum.elixirforum.com/u/tushar)\
**Post date:** [July 9, 2020, 6:51am UTC](https://forum.elixirforum.com/t/decoding-multi-level-json/32925/7 "2020-07-09T06:51:21Z")

</div>

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

```elixir
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

```elixir
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 ?

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [July 9, 2020, 6:54am UTC](https://forum.elixirforum.com/t/decoding-multi-level-json/32925/8 "2020-07-09T06:54:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tushar](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@tushar](https://forum.elixirforum.com/u/tushar)\
**Post date:** [July 9, 2020, 6:59am UTC](https://forum.elixirforum.com/t/decoding-multi-level-json/32925/9 "2020-07-09T06:59:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [July 9, 2020, 7:00am UTC](https://forum.elixirforum.com/t/decoding-multi-level-json/32925/10 "2020-07-09T07:00:49Z")

</div>

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](https://forum.elixirforum.com/t/decoding-multi-level-json/32925/6)?
