0

I have the following string:

"{\"headers\":[\"CNPJ\",\"PDF\",\"error\"],\"rows\":[[\"17192451000170\",\"FILE:application/pdf;170286;\",null],[\"234566767544\",\"FILE:application/pdf;456378;\",null],[\"233456767544\",\"FILE:application/pdf;456378;\",null]]}"

how do I parse it to a normal Json format?

meaning:

{"rows" :[
 {"CNPJ":"17192451000170","PDF":"FILE:application/pdf;170286;","error":null},
 {"CNPJ":"17192451000170","PDF":"FILE:application/pdf;170286;","error":null},
 {"CNPJ":"17192451000170", "PDF":"FILE:application/pdf;170286;,"error":null"}
]}

or any other json format

dina
  • 4,039
  • 6
  • 39
  • 67
  • Does your string *genuinely* have all those backslashes in, or is that what you're seeing in the debugger? – Jon Skeet Mar 07 '16 at 17:57
  • See also [this question](http://stackoverflow.com/questions/2395992/what-is-the-most-mature-json-library-for-erlang?lq=1), though the answers don't mention how to parse JSON into Erlang maps, which is what I presume you want to do given the expected output in your question. – legoscia Mar 07 '16 at 18:01
  • yes, my string genuinely has all those backslashes (it's not the debugger) – dina Mar 07 '16 at 18:01
  • for example with using [link jsx](https://github.com/talentdeficit/jsx) jsx:encode(<>). –  Mar 07 '16 at 18:29

1 Answers1

2

This is already a valid JSON format.

If you just want to strip \ then you can simply:

(hbd@crayon2.yoonka.com)31> JsonOrg = <<"{\"headers\":[\"CNPJ\",\"PDF\",\"error\"],\"rows\":[[\"17192451000170\",\"FILE:application/pdf;170286;\",null],[\"234566767544\",\"FILE:application/pdf;456378;\",null],[\"233456767544\",\"FILE:application/pdf;456378;\",null]]}">>.
<<"{\"headers\":[\"CNPJ\",\"PDF\",\"error\"],\"rows\":[[\"17192451000170\",\"FILE:application/pdf;170286;\",null],[\"234566767544\",\"FI"...>>

(hbd@crayon2.yoonka.com)32> io:format("~s~n", [binary_to_list(JsonOrg)]).
{"headers":["CNPJ","PDF","error"],"rows":[["17192451000170","FILE:application/pdf;170286;",null],["234566767544","FILE:application/pdf;456378;",null],["233456767544","FILE:application/pdf;456378;",null]]}
ok

You can also parse back and forth between Json and Erlang. I tested that with the yajler decoder:

(hbd@crayon2.yoonka.com)43> {ok, Parsed} = yajler:decode(<<"{\"headers\":[\"CNPJ\",\"PDF\",\"error\"],\"rows\":[[\"17192451000170\",\"FILE:application/pdf;170286;\",null],[\"234566767544\",\"FILE:application/pdf;456378;\",null],[\"233456767544\",\"FILE:application/pdf;456378;\",null]]}">>).
{ok,[{<<"headers">>,[<<"CNPJ">>,<<"PDF">>,<<"error">>]},
     {<<"rows">>,
      [[<<"17192451000170">>,<<"FILE:application/pdf;170286;">>,
        undefined],
       [<<"234566767544">>,<<"FILE:application/pdf;456378;">>,
        undefined],
       [<<"233456767544">>,<<"FILE:application/pdf;456378;">>,
        undefined]]}]}

(hbd@crayon2.yoonka.com)44> Json = binary:list_to_bin(yajler:encode(Parsed)).
<<"{\"headers\":[\"CNPJ\",\"PDF\",\"error\"],\"rows\":[[\"17192451000170\",\"FILE:application/pdf;170286;\",\"undefined\"],[\"2345667675"...>>

Yajler is an Erlang NIF so it is using a C library, in this case called yajl, to do the actual parsing, but I imagine a similar result you would get from other Erlang applications that can parse JSON.

Greg
  • 8,230
  • 5
  • 38
  • 53
  • here is the inverse problem: get json from string, but not vice versa. So you need to use the encode function. –  Mar 07 '16 at 18:43