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 Re: Building a JSON Show in Erlang
Date Tue, 04 Oct 2011 12:29:52 GMT
Paul,

Thank you for pointing me in the right direction. I was able to change the
JSON encoder. However I am still struggling to access the price and add it
to my new document. When I change from:

Price = couch_util:get_value(<<"price">>, Doc),

to

{Price} = couch_util:get_value(<<"price">>, Doc, {[]}),

I get the following error:

{"error": "badmatch","reason": "{'EXIT',{{badmatch,[{[{<<\"stores\">>,\n
[<<\"store_a\">>,<<\"store_b\">>,<<\"store_c\">>]},\n
{<<\"pricing\">>,\n
{[{<<\"regular\">>,10},{<<\"special\">>,5}]}}]},\n {[{<<\"stores\">>,\n
[<<\"store_c\">>,<<\"store_d\">>,<<\"store_e\">>]},\n
{<<\"pricing\">>,\n
{[{<<\"regular\">>,20},{<<\"special\">>,15}]}}]}]},\n [{erl_eval,expr,3}]}}"
}
*
*
*Note that the full representation of the source document I am trying to
access looks as follows:
*
*
*
*{"_id": "f0bb4f3208eb9c487e3d5f87b90005ec","_rev": "
3-8950f5ea245aa11f044ce11d4afca8f6","item_id": "sku_a","price": [{"stores":
["store_a","store_b","store_c"],"pricing": {"regular": 10,"special": 5}},{"
stores": ["store_c","store_d","store_e"],"pricing": {"regular": 20,"special
": 15}}]}
*
On Mon, Oct 3, 2011 at 10:18 PM, Paul Davis <paul.joseph.davis@gmail.com>wrote:

> Assuming you want the keys from price to be at the same level as item_id:
>
> Change this:
>
> > Price = couch_util:get_value(<<"price">>, Doc,{[]}),
>
> to:
>
> {Price} = couch_util:get_value(<<"price">>, Doc, {[]}),
>
> And this:
>
> > MyDoc = [{"item_id", DocId}, {"store_id",
> Store},{"price_count",PriceCount}],
>
> to:
>
> MyDoc = [{"item_id", DocId, {"store_id", Store}, {"price_count",
> PriceCount}] ++ Price,
>
>
> Also, its been awhile, but if memory serves, if you return the body as
> {<<"json">>, {MyDoc}} CouchDB will automatically ?JSON_ENCODE that
> into a string. This is good because if you're using newer Couches,
> you'll automatically get the speed improvements from using the C based
> encoders.
>
> HTH,
> Paul Davis
>
>
> > 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