Simplify Couchdb JSON response?

You can use show and _list functions, they take either a document or a view (respectively) and can send back a transformed response in whatever format you need. (in this case, JSON).

Up vote 5 down vote favorite 1 share g+ share fb share tw.

I'm storing location data in Couchdb, and am looking for a way to get an array of just the values, instead of key: value for every record. For example: The current response {"total rows": 250, "offset": 0, "rows": {"id": "ec5de6de2cf7bcac9a2a2a76de5738e4", "key": "user1", "value": {"city": "San Francisco", "address":"1001 Bayhill Dr"}, {"id": "ec5de6de2cf7bcac9a2a2a76de573ae4","key": "user1", "value": {"city": "Palo Alto", "address":"583 Waverley St"} ... (etc). } I only really need: {"city": "San Francisco", "address":"1001 Bayhill Dr"}, {"city": "Palo Alto", "address":"583 Waverley St"}, ... The reason for all this is to minimize the amount of bandwidth that a JSON response consumes.

I can't seem to find a way to transform the view into a simple array. Any suggestions? Thanks.

Couchdb link|improve this question asked Feb 9 '11 at 19:46Dick34229 71% accept rate.

You can use _show and _list functions, they take either a document or a view (respectively) and can send back a transformed response in whatever format you need. (in this case, JSON) Update: I ran a simple test with the data you provided here on my own CouchDB. Here's the list function I ended up writing.

Customize it to fit your needs. :) function (head, req) { // specify that we're providing a JSON response provides('json', function() { // create an array for our result set var results = ; while (row = getRow()) { results. Push({ city: row.value.

City, address: row.value. Address }); } // make sure to stringify the results :) send(JSON. Stringify(results)); }); }.

Great, thanks! I hadn't really looked at show/list functions yet since the Couchdb wiki suggests that their main purpose is to serve HTML directly (I was wrong). Btw, do you have an idea of the performance impact of using a show/list like this?

I'll run some benchmarks myself as well. – Dick Feb 9 '11 at 23:37 I don't have any numbers on hand. It'll probably be faster (in general) than templating on the browser-side.

Also, the built-in ETag support is a great caching mechanism to leverage. – Dominic Barnes Feb 10 '11 at 3:48 You could also send JSON within the while loop. Then send something before and after the loop to make it a valid JSON (and comma separate the objects).

Basically, send() can be called as times as you want and not just at the end of the function. – mikeycgto Jun 24 '11 at 18:56.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions