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 16:18:05 GMT
I figured it out. I had to do the following:

Price = couch_util:get_value(<<"price">>, Doc,[]),
MyDoc = {[{"item_id", DocId}, {"store_id", Store},{"price",Price}]},
{[{<<"code">>, 200}, {<<"headers">>, {[]}}, {<<"json">>,
MyDoc}]}

Thank y'all for the help.

On Tue, Oct 4, 2011 at 8:31 AM, Thomas Van de Velde <thomasevdv@gmail.com>wrote:

> Thank Bob. I tried that but then when I add the result to my existing list,
> I get an error.
>
> Price = couch_util:get_value(<<"price">>, Doc,[]),
>  MyDoc = [{"item_id", DocId}, {"store_id", Store}] ++ Price,
>
> results in:
>
> {"error": "unknown_error","reason": "function_clause"}
>
> On Tue, Oct 4, 2011 at 7:37 AM, Robert Newson <rnewson@apache.org> wrote:
>
>> "price" is an array, and your code is asserting that it's a tuple,
>> which is a badmatch.
>>
>> Price = couch_util:get_value(<<"price">>, Doc, []),
>>
>> B.
>>
>> On 4 October 2011 13:29, Thomas Van de Velde <thomasevdv@gmail.com>
>> wrote:
>> > 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