incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Van de Velde <thomase...@gmail.com>
Subject Building a JSON Show in Erlang
Date Tue, 04 Oct 2011 01:08:34 GMT
Hi,

I am almost done converting an existing JavaScript show function to Erlang.
Hopefully somebody can help me get over this last hump. I am trying to copy
a section of my JSON document into a new JSON document. I am having trouble
copying a nested structure from the document into my new document. Price
below is a nested JSON structure e.g.: "price" : {"sale":20, "regular": 30}.
While I can pull individual field values and explicitly set them in MyDoc, I
am stuck putting everything under "price" in this structure. I'd like to
avoid dealing with the pricing fields individually and simply move
everything under that field into the target JSON document. Any help is
greatly appreciated. Thomas

fun({Doc}, {Req}) ->
{Query} = couch_util:get_value(<<"query">>,Req, {[]}),
Store = couch_util:get_value(<<"store">>,Query),
DocId = couch_util:get_value(<<"item_id">>, Doc),"
Price = couch_util:get_value(<<"price">>, Doc,{[]}),
PriceCount = length(Price),
MyDoc = [{"item_id", DocId}, {"store_id",
Store},{"price_count",PriceCount}], % How do I add the nested price document
here?
Encoded = mochijson2:encode({struct, MyDoc}),
{[{<<"code">>, 200}, {<<"headers">>, {[{<<"Content-Type">>,
<<"text/plain">>}]}}, {<<"body">>, Encoded}]}
end.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message