In a recent project ive worked on, we were dealing with big JSON data structures coming from EXT JS front end apps. One example of the JSON object is here below (this is just the skeleton of the JSON ):
{
"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"
}
]
}
After mochijson2:decode/1
, the struct object i had appears like this:
{struct,[{<<"presence_token">>,
<<"734737328233HDHSBSHSYEYEYWYWGWE">>},
{<<"presence_time">>,<<"HH:Mins:Secs">>},
{<<"friend_requests">>,
[{struct,[{<<"from">>,<<"Username">>},
{<<"type">>,<<"buddy">>},
{<<"date">>,<<"DD/MM/YY">>},
{<<"time">>,<<"HH:Mins:Secs">>},
{<<"name">>,<<"Your Full name">>},
{<<"email">>,<<"user@example.com">>}]}]},
{<<"group_status">>,
[{struct,[{<<"group_name">>,<<"ecampus">>},
{<<"status">>,<<"running">>},
{<<"members">>,[<<"phil">>,<<"josh">>,<<"shazz">>]},
{<<"start_date">>,<<"DD/MM/YY">>},
{<<"start_time">>,<<"HH:Mins:Secs">>}]},
{struct,[{<<"group_name">>,<<"buganda">>},
{<<"status">>,<<"off">>}]}]},
{<<"friend_status">>,
[{struct,[{<<"friend">>,<<"Friend_username">>},
{<<"status">>,<<"online">>},
{<<"log_on_time">>,<<"HH:Mins:Secs">>},
{<<"state">>,<<"available">>},
{<<"name">>,<<"Friend_Fullname">>},
{<<"email">>,<<"user@example.com">>}]},
{struct,[{<<"friend">>,<<"Friend_username">>},
{<<"status">>,<<"offline">>},
{<<"name">>,<<"Friend_Fullname">>},
{<<"email">>,<<"user@example.com">>}]}]}]}
Now i decided to create a module which will convert this struct into a "deep" proplist, this module would contain a function struct:all_keys/1
which if i feed it with the struct object it generates lists and tuples in an organised way. Here is the code:
-module(struct).
-export([all_keys/1]).
is_struct({struct,_}) -> true;
is_struct(_) -> false.
to_binary(S) when is_list(S)-> list_to_binary(S);
to_binary(S) when is_integer(S)-> S;
to_binary(S) when is_atom(S)-> to_binary(atom_to_list(S));
to_binary(S) -> S.
to_value(V) when is_binary(V)-> binary_to_list(V);
to_value(V) when is_integer(V)-> V;
to_value(V) when is_list(V)->
try list_to_integer(V) of
PP -> PP
catch
_:_ ->
try list_to_float(V) of
PP2 -> PP2
catch
_:_ -> V
end
end;
to_value(A)-> A.
to_value2({struct,L})->
all_keys({struct,L});
to_value2([{struct,_L}|_Rest] = LL)->
[all_keys(XX) || XX <- LL];
to_value2(D) when is_binary(D)-> to_value(D);
to_value2(D) when is_list(D)->
[to_value2(Any) || Any <- D].
all_keys({struct,L})->
[{to_value(Key),to_value2(Val)} || {Key,Val} <- L];
all_keys(List)-> [all_keys(X) || X <- List].
Now, calling struct:all_keys(Struct_object)
will give this output:
[{"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"}]]}]
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