1

I have some code that get a list of file from a directory and I want to send it back as a json to the client

getDir('GET', [])-> 
    {ok,Sheets} = files(FileDir),
    Sheets2 = sheetJson(Sheets, ""),
    {json, [{sheets, Sheets2}]}.

sheetJson([H|T], [])->
    {File, Name, Size} = H,
    Str = [{file, File},{name, Name}],
    sheetJson(T, Str);

sheetJson([H|T], Str)->
    {File, Name, Size} = H,
    Acc = Str++[{file, File},{name, Name}],
    sheetJson(T, Acc);

sheetJson(_, Str)->Str.

this code will return

{"sheets":{"file":"0-Jason .csv","name":"Jason ","file":"1-State.csv","name":"State","file":"2-country.csv","name":"country"}}

But was wanting something more like

 {"sheets":{"file":"0-Jason .csv","name":"Jason "},{"file":"1-State.csv","name":"State"},{"file":"2-country.csv","name":"country"}}

and if I try changing the code to anything then that I get error messages like

Unhandled Error: error:function_clause. Stacktrace: [{boss_json,json_data1,[[{sheet,{file,"0-Sheet1.csv"},{name,"Sheet1"}}],[],[]],[{file,"src/boss/boss_json.erl"},{line,31}]},{boss_json,json_data1,3,[{file,"src/boss/boss_json.erl"},{line,42}]},{boss_json,encode,2,[{file,"src/boss/boss_json.erl"},{line,16}]},{boss_web_controller_render,process_action_result,4,[{file,"src/boss/boss_web_controller_render.erl"},{line,171}]},{boss_web_controller,execute_action_inner,9,[{file,"src/boss/boss_web_controller.erl"},{line,337}]},{boss_web_controller_handle_request,process_dynamic_request,4,[{file,"src/boss/boss_web_controller_handle_request.erl"},{line,242}]},{boss_web_controller_handle_request,process_request,4,[{file,"src/boss/boss_web_controller_handle_request.erl"},{line,228}]},{boss_web_controller_handle_request,set_timer,7,[{file,"src/boss/boss_web_controller_handle_request.erl"},{line,148}]}]

Edit : Solution I came up with

I just ended up just writing a json String and using {output, _} instead of {json, _}, the benefit was I could see the Json string in the javascript error and fix, where as with erlang I just get a massive message that tells me nothing.

jazzjazzy
  • 412
  • 3
  • 20

1 Answers1

1

The following JSON you are trying to produce is actually invalid:

{"sheets":{"file":"0-Jason .csv","name":"Jason "},{"file":"1-State.csv","name":"State"},{"file":"2-country.csv","name":"country"}}

Instead, you probably want to encode as follows:

{"sheets":[{"file":"0-Jason .csv","name":"Jason "},{"file":"1-State.csv","name":"State"},{"file":"2-country.csv","name":"country"}]}

This will simply be achieved by creating a list of sheets. Basically, your function getDir/2 should return the following value:

{json, [{sheets, [
    [{file, "0-Jason .csv"}, {"name", "Jason "}],
    [{file, "1-State.csv"}, {"name", "State"}],
    [{file, "1-country.csv"}, {"name", "country"}]
]}]}.

You probably just need to change two lines:

sheetJson([H|T], [])->
    {File, Name, Size} = H,
    Str = [[{file, File},{name, Name}]],  %%% <-- here
    sheetJson(T, Str);

sheetJson([H|T], Str)->
    {File, Name, Size} = H,
    Acc = Str++[[{file, File},{name, Name}]],   %%% <-- here
    sheetJson(T, Acc);
Paul Guyot
  • 6,257
  • 1
  • 20
  • 31
  • I realized the Json was not correct after I put it up, but I tried so many variation forgot which one to use, I tried changing it as you suggested but I still got the same error, see edit for how I got around the problem – jazzjazzy Jul 21 '14 at 01:33