Problems, need help? Have a tip or advice? Post it here.
2 posts Page 1 of 1
How to generate a JSON request from a couch template?

If the data output is in the format:

Code: Select all
{"doctorList":[{"reviews":"Nice","sex":"Male","subCategory":"ONCOLOGIST","speciality":"CANCER","timings":"10AM-5PM","password":null,"id":1,"qualification":"MD","email":"madan@gmail.com","address":"Nagpur","age":32,"name":"Dr. Madan","longitude":22.3,"experience":3,"rating":2.5,"lat":21.9,"mobileNo":"9090909090","regId":123},{"reviews":"Good","sex":"Male","subCategory":"GENERAL","speciality":"COLD","timings":"10:00 AM - 12:00 PM","password":null,"id":2,"qualification":"MBBS","email":"akshay.patre@gmail.com","address":"74. shahu nagar","age":42,"name":"Dr.Akshay","longitude":67,"experience":10,"rating":2,"lat":92,"mobileNo":"9823051718","regId":356788},{"reviews":"Ok","sex":"Male","subCategory":"COSMETOLOGIST","speciality":"SKIN","timings":"10:00 AM - 12:00 PM","password":null,"id":3,"qualification":"MD","email":"yusuf@gmail.com","address":"Wardha","age":26,"name":"Dr.Yusuf","longitude":67,"experience":4,"rating":2,"lat":92,"mobileNo":"9823461052","regId":100},{"reviews":"Good","sex":"Male","subCategory":"ONCOLOGIST","speciality":"CANCER","timings":"10AM-5PM","password":null,"id":4,"qualification":"MD","email":"ankit.rathi@gmail.com","address":"Patna","age":32,"name":"Dr. Rathi","longitude":22.3,"experience":9,"rating":2.5,"lat":21.9,"mobileNo":"9090909090","regId":909},{"reviews":"GOOD","sex":"Male","subCategory":"ONCOLOGIST","speciality":"CANCER","timings":"10AM - 5AM","password":null,"id":5,"qualification":"MS","email":"jaipy.fabled88@gmail.com","address":"Flat No-3, Rawal Apartments","age":26,"name":"Hemant Varhekar","longitude":79,"experience":3,"rating":2,"lat":21,"mobileNo":"9730687666","regId":10}]}


the above is a GET request!

Regards,
GenXCoders
Image
where innovation meets technology
Hi,

Assuming the data is contained in cloneable pages of some Couch template, you can use cms:pages to loop through the pages and craft the JSON output.

Following is a very simple example (from @cheesypoof's solution viewtopic.php?f=3&t=7869) that outputs all the titles in JSON format.
Code: Select all
<cms:content_type 'application/json'/>

[
    <cms:pages masterpage='blog.php'>
        "<cms:addslashes><cms:show k_page_title/></cms:addslashes>"<cms:if "<cms:not k_paginated_bottom/>">,</cms:if>
    </cms:pages>
]

Three points that need attention -
1. You'll have to use cms:content_type at the very top to set the documents type to JSON
2. Use cms:addslashes to escape any quotes in the values
3. Take care *not* to output the last comma (in the example above this is done by checking for 'k_paginated_bottom').

Not being careful with the last two points will result in invalid JSON format.

For a more involved example, please see another @cheesypoof's thread - viewtopic.php?f=8&t=8602

Hope it helps.
2 posts Page 1 of 1