JSON encoding of arrays of arrays

Steve Davis steven.charles.davis@REDACTED
Thu Oct 21 15:32:43 CEST 2010


The json syntax looks good, but I think this is an array of objects
which contain arrays of objects.

One way to encode an object would be to map it as a proplist, so an
array of objects ends up as a list of proplists:

[[{<<"words">>,[[{<<"word">>,<<"someword1">>},
                 {<<"frequency">>,10}],
                [{<<"word">>,<<"someword2">>},{<<"frequency">>,8}],
                [{<<"word">>,<<"someword3">>},{<<"frequency">>,3}]]}],
       [{<<"words">>,[[{<<"word">>,<<"someword1">>},{<<"frequency">>,
2}],
                [{<<"word">>,<<"someword2">>},{<<"frequency">>,1}],
                [{<<"word">>,<<"someword3">>},{<<"frequency">>,5}]]}],
       [{<<"words">>,[[{<<"word">>,<<"someword1">>},{<<"frequency">>,
4}],
                [{<<"word">>,<<"someword2">>},{<<"frequency">>,9}],
                [{<<"word">>,<<"someword3">>},{<<"frequency">>,
12}]]}]].

/s

On Oct 21, 7:09 am, Alessandro Sivieri <alessandro.sivi...@REDACTED>
wrote:
> Hi all,
>
> I'm struggling to find out how to properly write a structure that my
> MochiWeb server has to respond to a client (in JSON). The final JSON (if I
> have correctly written it) should be as follows:
>
> [
>     {
>         "words":
>   [
>       {"word": "someword1", "frequency": 10},
>        {"word": "someword2", "frequency": 8},
>       {"word": "someword3", "frequency": 3}
>   ]
>     },
> {
>         "words":
>           [
>               {"word": "someword1", "frequency": 2},
>               {"word": "someword2", "frequency": 1},
>               {"word": "someword3", "frequency": 5}
>           ]
>     },
> {
>         "words":
>           [
>               {"word": "someword1", "frequency": 4},
>               {"word": "someword2", "frequency": 9},
>               {"word": "someword3", "frequency": 12}
>           ]
>     }
> ]
>
> It comes from a word frequency analysis of some documents: each "words"
> element represents the output of a single document, and for each of them
> there is a list of words with their frequencies. Now, how should I write the
> struct in Erlang (given the fact that I am starting from a list of tuples
> for each document), so that mochijson2 produces the previous JSON?
> I have searched on the Web, many resources point to a video tutorial but it
> does not use a list of lists, so I have not been able to find out a way to
> write it...
>
> --
> Sivieri Alessandro
> alessandro.sivi...@REDACTED://www.chimera-bellerofonte.eu/http://www.poul.org/


More information about the erlang-questions mailing list