You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Michael McCaffrey <mm...@ittvis.com> on 2009/08/31 20:01:52 UTC

valid json string for mochijson2

Hello,

 

I'm trying to get a couchdb function to return a simple json doc and I
can't get it to validate thru mochijson2.encoder.

 

Just trying to return {"fld","data"} and get the
{"error":"json_encode","reason":"{bad_term,{<<\"fld\">>,<<\"data\">>}}"}
error

 

Running the encoder I see it expects {[{"fld","data"}]}  format which I
cannot get the couchdb function to produce.

 

Any help on how to format this list of json docs in a couchdb function? 

 

Thanks 

 

 

 

 


RE: valid json string for mochijson2

Posted by Nils Breunese <N....@vpro.nl>.
It might just be a typo, but {"fld","data"} is not valid JSON. You probably want/mean {"fld":"data"} or ["fld","data"].

Nils Breunese.
________________________________________
Van: Michael McCaffrey [mmccaffrey@ittvis.com]
Verzonden: maandag 31 augustus 2009 20:01
Aan: user@couchdb.apache.org
Onderwerp: valid json string for mochijson2

Hello,



I'm trying to get a couchdb function to return a simple json doc and I
can't get it to validate thru mochijson2.encoder.



Just trying to return {"fld","data"} and get the
{"error":"json_encode","reason":"{bad_term,{<<\"fld\">>,<<\"data\">>}}"}
error



Running the encoder I see it expects {[{"fld","data"}]}  format which I
cannot get the couchdb function to produce.



Any help on how to format this list of json docs in a couchdb function?



Thanks










De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan.

RE: valid json string for mochijson2

Posted by Michael McCaffrey <mm...@ittvis.com>.
Oh, don't give me any credit, it's all my compatriot Norm here who's
come up with this. I'm just hacking up his code...

He's away this week so I'm bothering you guys :)

I'll pursue the pattern matching you mentioned.

Mike

-----Original Message-----
From: Paul Davis [mailto:paul.joseph.davis@gmail.com] 
Sent: Monday, August 31, 2009 2:24 PM
To: user@couchdb.apache.org
Subject: Re: valid json string for mochijson2

> {FormResp} = csw_view_query_servers:render_view(Lang, UpFun, nil, nil,
Req, Db),
> couch_httpd:send_json(Req, FormResp);

You appear to be pattern matching the response into invalid ErlJSON
here. Try removing the curly braces around FormResp.

Nice use of _show mechanics for that. Didn't even cross my mind when
contemplating it.

Paul

RE: valid json string for mochijson2

Posted by Michael McCaffrey <mm...@ittvis.com>.
Thanks for your help, I got it working.

I can see now where it might be a problem since the FormResp was a tuple

Much to learn here.

Thanks.

-----Original Message-----
From: Paul Davis [mailto:paul.joseph.davis@gmail.com] 
Sent: Monday, August 31, 2009 2:24 PM
To: user@couchdb.apache.org
Subject: Re: valid json string for mochijson2

> {FormResp} = csw_view_query_servers:render_view(Lang, UpFun, nil, nil,
Req, Db),
> couch_httpd:send_json(Req, FormResp);

You appear to be pattern matching the response into invalid ErlJSON
here. Try removing the curly braces around FormResp.

Nice use of _show mechanics for that. Didn't even cross my mind when
contemplating it.

Paul

Re: valid json string for mochijson2

Posted by Paul Davis <pa...@gmail.com>.
> {FormResp} = csw_view_query_servers:render_view(Lang, UpFun, nil, nil, Req, Db),
> couch_httpd:send_json(Req, FormResp);

You appear to be pattern matching the response into invalid ErlJSON
here. Try removing the curly braces around FormResp.

Nice use of _show mechanics for that. Didn't even cross my mind when
contemplating it.

Paul

RE: valid json string for mochijson2

Posted by Michael McCaffrey <mm...@ittvis.com>.
Here's the code I've stolen from the conversion process. Granted, this used to be a POST routine that I've converted to a GET for my testing/learning purposes...


{FormResp} = csw_view_query_servers:render_view(Lang, UpFun, nil, nil, Req, Db),
couch_httpd:send_json(Req, FormResp); 


-module(csw_view_query_servers)
render_view(Lang, UpdateFun, DocId, Doc, Req, Db) ->
    Pid = get_os_process(Lang),
    {JsonReqIn} = couch_httpd_external:json_req_obj(Req, Db),

    {JsonReq, JsonDoc} = case {DocId, Doc} of
        {nil, nil} -> {{JsonReqIn}, null};
        {DocId, nil} -> {{append_docid(DocId, JsonReqIn)}, null};
        _ -> {{append_docid(DocId, JsonReqIn)}, couch_doc:to_json_obj(Doc, [revs])}
    end,

	try couch_os_process:prompt(Pid, [<<"show_doc">>, UpdateFun, JsonDoc, JsonReq]) of
        FormResp ->
            FormResp
    after
        ok = ret_os_process(Lang, Pid)
    end.


If it requires a erlJSON format, how does one produce this out of couchdb function? 

Thanks again.

-----Original Message-----
From: Paul Davis [mailto:paul.joseph.davis@gmail.com] 
Sent: Monday, August 31, 2009 2:00 PM
To: user@couchdb.apache.org
Subject: Re: valid json string for mochijson2

> So I've got a simple function that just returns a json doc.
> {
>   "getData": "function(doc){ return {\"fld\":\"data\"};}"
> }
>
> It is called from an erlang module which gets back a FormResp and then calls
>
> couch_httpd:send_json(Req, FormResp)

Can you paste this bit of Erlang that you're using? If you're using
the map docs interface, then it should be returned as a proper ErlJSON
object but its hard to guess.

Paul

Re: valid json string for mochijson2

Posted by Paul Davis <pa...@gmail.com>.
> So I've got a simple function that just returns a json doc.
> {
>   "getData": "function(doc){ return {\"fld\":\"data\"};}"
> }
>
> It is called from an erlang module which gets back a FormResp and then calls
>
> couch_httpd:send_json(Req, FormResp)

Can you paste this bit of Erlang that you're using? If you're using
the map docs interface, then it should be returned as a proper ErlJSON
object but its hard to guess.

Paul

RE: valid json string for mochijson2

Posted by Michael McCaffrey <mm...@ittvis.com>.
We're going to be creating some custom views/functions and calling them from erlang modules. To this end, I'm just calling a function on the design document that we've created. 

Since I'm new to erlang/couch I'm just testing a very simple function being called from an erlang module via a handler and daemon. This is based on a process that we used to bulk load data during a conversion. 

So I've got a simple function that just returns a json doc.
{
   "getData": "function(doc){ return {\"fld\":\"data\"};}"
}

It is called from an erlang module which gets back a FormResp and then calls

couch_httpd:send_json(Req, FormResp)


[error] [<0.278.0>] Uncaught error in HTTP request: {exit,
                                 {json_encode,
                                  {bad_term,{<<"fld">>,<<"data">>}}}}
[debug] [<0.278.0>] Stacktrace: [{mochijson2,json_encode,2},
             {mochijson2,'-json_encode_array/2-fun-0-',3},
             {lists,foldl,3},
             {mochijson2,json_encode_array,2},
             {couch_httpd,send_json,4},
             {couch_httpd_db,do_db_req,2},
             {couch_httpd,handle_request,4},
             {mochiweb_http,headers,5}]
[debug] [<0.278.0>] httpd 500 error response:
 {"error":"json_encode","reason":"{bad_term,{<<\"fld\">>,<<\"data\">>}}"}


Running that {\"fld\":\"data\"} json document thru mochijson2.decode returns
{[{<<\"fld\">>,<<\"data\">>}]} so my thought is that I need to convert the document returned from couch function into a similar format.

Perhapos there's something else format-wise that the _views functions do when returning data?

Thanks




-----Original Message-----
From: Paul Davis [mailto:paul.joseph.davis@gmail.com] 
Sent: Monday, August 31, 2009 12:52 PM
To: user@couchdb.apache.org
Subject: Re: valid json string for mochijson2

Michael,

Are you working in Erlang with something? If so, could you paste a
short snippet of what you're trying to accomplish?

Paul Davis

On Mon, Aug 31, 2009 at 2:01 PM, Michael McCaffrey<mm...@ittvis.com> wrote:
> Hello,
>
>
>
> I'm trying to get a couchdb function to return a simple json doc and I
> can't get it to validate thru mochijson2.encoder.
>
>
>
> Just trying to return {"fld","data"} and get the
> {"error":"json_encode","reason":"{bad_term,{<<\"fld\">>,<<\"data\">>}}"}
> error
>
>
>
> Running the encoder I see it expects {[{"fld","data"}]}  format which I
> cannot get the couchdb function to produce.
>
>
>
> Any help on how to format this list of json docs in a couchdb function?
>
>
>
> Thanks
>
>
>
>
>
>
>
>
>
>

Re: valid json string for mochijson2

Posted by Paul Davis <pa...@gmail.com>.
Michael,

Are you working in Erlang with something? If so, could you paste a
short snippet of what you're trying to accomplish?

Paul Davis

On Mon, Aug 31, 2009 at 2:01 PM, Michael McCaffrey<mm...@ittvis.com> wrote:
> Hello,
>
>
>
> I'm trying to get a couchdb function to return a simple json doc and I
> can't get it to validate thru mochijson2.encoder.
>
>
>
> Just trying to return {"fld","data"} and get the
> {"error":"json_encode","reason":"{bad_term,{<<\"fld\">>,<<\"data\">>}}"}
> error
>
>
>
> Running the encoder I see it expects {[{"fld","data"}]}  format which I
> cannot get the couchdb function to produce.
>
>
>
> Any help on how to format this list of json docs in a couchdb function?
>
>
>
> Thanks
>
>
>
>
>
>
>
>
>
>