Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In a recent project ive worked on, we were dealing with big JSON data structures coming from <b><a href="http://docs.sencha.com/ext-js/4-0/" rel="nofollow">EXT JS</a></b> front end apps. One example of the JSON object is here below (this is just the skeleton of the JSON ):</p> <pre> { "presence_token":"734737328233HDHSBSHSYEYEYWYWGWE", "presence_time":"HH:Mins:Secs", "friend_requests": [ { "from":"Username", "type":"buddy", "date":"DD/MM/YY", "time":"HH:Mins:Secs", "name":"Your Full name", "email":"user@example.com" } ], "group_status": [ { "group_name":"ecampus", "status":"running", "members":["phil","josh","shazz"], "start_date":"DD/MM/YY", "start_time":"HH:Mins:Secs" }, { "group_name":"buganda", "status":"off" } ], "friend_status": [ { "friend":"Friend_username", "status":"online", "log_on_time":"HH:Mins:Secs", "state":"available", "name":"Friend_Fullname", "email":"user@example.com" }, { "friend":"Friend_username", "status":"offline", "name":"Friend_Fullname", "email":"user@example.com" } ] } </pre> <p>After <code>mochijson2:decode/1</code>, the struct object i had appears like this:</p> <pre> {struct,[{&lt;&lt;"presence_token"&gt;&gt;, &lt;&lt;"734737328233HDHSBSHSYEYEYWYWGWE"&gt;&gt;}, {&lt;&lt;"presence_time"&gt;&gt;,&lt;&lt;"HH:Mins:Secs"&gt;&gt;}, {&lt;&lt;"friend_requests"&gt;&gt;, [{struct,[{&lt;&lt;"from"&gt;&gt;,&lt;&lt;"Username"&gt;&gt;}, {&lt;&lt;"type"&gt;&gt;,&lt;&lt;"buddy"&gt;&gt;}, {&lt;&lt;"date"&gt;&gt;,&lt;&lt;"DD/MM/YY"&gt;&gt;}, {&lt;&lt;"time"&gt;&gt;,&lt;&lt;"HH:Mins:Secs"&gt;&gt;}, {&lt;&lt;"name"&gt;&gt;,&lt;&lt;"Your Full name"&gt;&gt;}, {&lt;&lt;"email"&gt;&gt;,&lt;&lt;"user@example.com"&gt;&gt;}]}]}, {&lt;&lt;"group_status"&gt;&gt;, [{struct,[{&lt;&lt;"group_name"&gt;&gt;,&lt;&lt;"ecampus"&gt;&gt;}, {&lt;&lt;"status"&gt;&gt;,&lt;&lt;"running"&gt;&gt;}, {&lt;&lt;"members"&gt;&gt;,[&lt;&lt;"phil"&gt;&gt;,&lt;&lt;"josh"&gt;&gt;,&lt;&lt;"shazz"&gt;&gt;]}, {&lt;&lt;"start_date"&gt;&gt;,&lt;&lt;"DD/MM/YY"&gt;&gt;}, {&lt;&lt;"start_time"&gt;&gt;,&lt;&lt;"HH:Mins:Secs"&gt;&gt;}]}, {struct,[{&lt;&lt;"group_name"&gt;&gt;,&lt;&lt;"buganda"&gt;&gt;}, {&lt;&lt;"status"&gt;&gt;,&lt;&lt;"off"&gt;&gt;}]}]}, {&lt;&lt;"friend_status"&gt;&gt;, [{struct,[{&lt;&lt;"friend"&gt;&gt;,&lt;&lt;"Friend_username"&gt;&gt;}, {&lt;&lt;"status"&gt;&gt;,&lt;&lt;"online"&gt;&gt;}, {&lt;&lt;"log_on_time"&gt;&gt;,&lt;&lt;"HH:Mins:Secs"&gt;&gt;}, {&lt;&lt;"state"&gt;&gt;,&lt;&lt;"available"&gt;&gt;}, {&lt;&lt;"name"&gt;&gt;,&lt;&lt;"Friend_Fullname"&gt;&gt;}, {&lt;&lt;"email"&gt;&gt;,&lt;&lt;"user@example.com"&gt;&gt;}]}, {struct,[{&lt;&lt;"friend"&gt;&gt;,&lt;&lt;"Friend_username"&gt;&gt;}, {&lt;&lt;"status"&gt;&gt;,&lt;&lt;"offline"&gt;&gt;}, {&lt;&lt;"name"&gt;&gt;,&lt;&lt;"Friend_Fullname"&gt;&gt;}, {&lt;&lt;"email"&gt;&gt;,&lt;&lt;"user@example.com"&gt;&gt;}]}]}]} </pre> <p>Now i decided to create a module which will convert this struct into a "deep" proplist, this module would contain a function <code>struct:all_keys/1</code> which if i feed it with the struct object it generates lists and tuples in an organised way. Here is the code:</p> <pre> -module(struct). -export([all_keys/1]). is_struct({struct,_}) -&gt; true; is_struct(_) -&gt; false. to_binary(S) when is_list(S)-&gt; list_to_binary(S); to_binary(S) when is_integer(S)-&gt; S; to_binary(S) when is_atom(S)-&gt; to_binary(atom_to_list(S)); to_binary(S) -&gt; S. to_value(V) when is_binary(V)-&gt; binary_to_list(V); to_value(V) when is_integer(V)-&gt; V; to_value(V) when is_list(V)-&gt; try list_to_integer(V) of PP -&gt; PP catch _:_ -&gt; try list_to_float(V) of PP2 -&gt; PP2 catch _:_ -&gt; V end end; to_value(A)-&gt; A. to_value2({struct,L})-&gt; all_keys({struct,L}); to_value2([{struct,_L}|_Rest] = LL)-&gt; [all_keys(XX) || XX &lt;- LL]; to_value2(D) when is_binary(D)-&gt; to_value(D); to_value2(D) when is_list(D)-&gt; [to_value2(Any) || Any &lt;- D]. all_keys({struct,L})-&gt; [{to_value(Key),to_value2(Val)} || {Key,Val} &lt;- L]; all_keys(List)-&gt; [all_keys(X) || X &lt;- List]. </pre> <p>Now, calling <code>struct:all_keys(Struct_object)</code> will give this output:</p> <pre> [{"presence_token",P_token}, {"presence_time",P_time}, {"friend_requests", [[{"from","Username"}, {"type","buddy"}, {"date","DD/MM/YY"}, {"time","HH:Mins:Secs"}, {"name","Your Full name"}, {"email","user@example.com"}]]}, {"group_status", [[{"group_name","ecampus"}, {"status","running"}, {"members",["phil","josh","shazz"]}, {"start_date","DD/MM/YY"}, {"start_time","HH:Mins:Secs"}], [{"group_name","buganda"},{"status","off"}]]}, {"friend_status", [[{"friend","Friend_username"}, {"status","online"}, {"log_on_time","HH:Mins:Secs"}, {"state","available"}, {"name","Friend_Fullname"}, {"email","user@example.com"}], [{"friend","Friend_username"}, {"status","offline"}, {"name","Friend_Fullname"}, {"email","user@example.com"}]]}] </pre> <p>The above proplist is easier to work with than the struct object. However, you may find another version of the struct module especially in a famous mochiweb example called Sticky Notes whose link i do not have right now.The struct module i have pasted above should be able to help you with using mochijson2. success</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload