Simplify Couchdb JSON response -
i'm storing location data in couchdb, , looking way array of values, instead of key: value every record. 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 need:
[{"city": "san francisco", "address":"1001 bayhill dr"}, {"city": "palo alto", "address":"583 waverley st"}, ...]
the reason minimize amount of bandwidth json response consumes. can't seem find way transform view simple array. suggestions?
thanks.
you can use _show , _list functions, take either document or view (respectively) , can send transformed response in whatever format need. (in case, json)
update: ran simple test data provided here on own couchdb. here's list function ended writing. customize fit needs. :)
function (head, req) { // specify we're providing json response provides('json', function() { // create array our result set var results = []; while (row = getrow()) { results.push({ city: row.value.city, address: row.value.address }); } // make sure stringify results :) send(json.stringify(results)); }); }
Comments
Post a Comment