Jason decode twice to pattern match correctly

236 Views Asked by At

I'm really new to elixir, so forgive my ignorance. I have a json response in body (coming from a public api) that i need to decode twice to actually get a map that i can pattern match. I do not understand why is that happening and how to avoid it? Thanks.

body = "\"{\\\"access_token\\\":\\\"r5f3w73pjjjoembe4azhp892475wah\\\",\\\"expires_in\\\":5102694,\\\"token_type\\\":\\\"bearer\\\"}\\n\""
result = body |> Jason.decode! |> Jason.decode!
%{"access_token" => access_token} = result

Edit: The response is alright in Postman/Node fetch, it's just escaped like that when it's coming from the Tesla client in elixir.

1

There are 1 best solutions below

0
On BEST ANSWER

As i thought, my ignorance was to blame. I didn't know that inspect() is actually Kernel.inspect() (which actually makes sense) and that function outputs a modified representation of the input for debugging. I confused it with IO.inspect() which returns an unchanged item. So I'll leave this question up in case anyone mixed them up too.