raich
raich

Reputation: 127

Erlang jsx:encode remove escape backslash

I'm calling a Api Service that has the following json requirement:

    {
       "user": {
         "userid": "123456"  
       },
      "access_token": "ABCDEFGHIJKLMPNOPQRST"
    }

I'm doing the following in my code:

   MyUser =  {<<"uid">>, <<"MyId-1">>},
   Body = json_body_([{{<<"user">>, MyUser},{<<"access_token">>, <<?ORGANIZATION_ACCESS_TOKEN>>}}]),
   Body1 = lists:map(fun erlang:tuple_to_list/1, Body), 
   io:format("Body in start : ~n~p~n", [Body1]).

    json_body_(ParamList) ->
        json_body__(ParamList, []).

    json_body__([], Acc) ->
        jsx:encode(lists:reverse(Acc));
    json_body__([{K, V} | Rest], Acc) ->
        Acc1 = [{sanitize_(K), sanitize_(V)} | Acc],
    json_body__(Rest, Acc1).
    sanitize_(Parm) ->
        Parm.

When I apply jsx:enocode to "Body1" the result is:

    [{\"user\":{\"uid\":\"My-id-1234\"},\"access_token\":\"12345678ff4089\"}]

How can I get rid of the escape "\"?

Upvotes: 0

Views: 724

Answers (1)

Dogbert
Dogbert

Reputation: 222398

Your string doesn't contain any \. Since you printed using ~p, Erlang escaped every double quote in the string to make the final output valid Erlang code. You can verify this by printing using ~s instead.

1> S = "{\"foo\": \"bar\"}".
"{\"foo\": \"bar\"}"
2> io:format("~p~n", [S]).
"{\"foo\": \"bar\"}"
ok
3> io:format("~s~n", [S]).
{"foo": "bar"}
ok

Upvotes: 3

Related Questions