1
\$\begingroup\$

I needed quickly to represent a list in json format. I wrote the following front-end in jinja2 with google appengine.

{
"title": "The Basics - Networking",
"description": "Your app fetched this from a remote endpoint!",
"movies": [

{% if results %}
    {% for scored_document in results %}

        { "title": "{{ scored_document.fields.0.value|safe | replace('"','') }}", "releaseYear": "2014",
 {% set testing = scored_document.fields.8.value|displayimg %}
                                    {% if testing and testing != 'False' %}
        "img": "{{ testing }}",
   {% endif %}
        "url": "https://www.koolbusiness.com/newvi/{{ scored_document.fields.8.value|int|safe }}.html"}{% if not loop.last %}
        ,
    {% endif %}
    {% endfor %}{% endif %}
]
}

There are several unclean errors with wrong variable names because I copied the code from an example but the output is good:

{ "title": "The Basics - Networking", "description": "Your app fetched this from a remote endpoint!", "movies": [ { "title": "1200 SQFT SITES available for sale Nelamangala for sale--7lacs-30*40 sqft ", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/wEuEmcPNEXfA37EATiWaQ0odK09nAjcaiL5HR_WBCr5RTyU6LeKkfSHH9kQkcchfRToRE4z7UKsfrOtlZnwTxmMI2Xc=s150", "url": "https://www.koolbusiness.com/newvi/6371047123189760.html"} , { "title": "Buy prestige glass top 3 burner gas stove from nbhomeshop", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/_rL9pl62uSq4g9csH1aBK1cniie_JUEAZwUIKQAxlhjIH875O1D4aVVgV6blxGECgsOvSRcL15pdk3-JAmEI_zltcw=s150", "url": "https://www.koolbusiness.com/newvi/6469401068961792.html"} , { "title": "angular js online training,software courses,android course,seo course,Corporate training institutes in Hyderabad.", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/mcih7gjnhgF-R6--Kj45X16U7j2GjpQhI5_JaU3XOS_cIBIrQbhHb14rseIlyqSUZALDj3g-ofXKhm_JgvMnP0ns8Q=s150", "url": "https://www.koolbusiness.com/newvi/6735288535613440.html"} , { "title": "angular js online training,software courses,android course,seo course,Corporate training institutes in Hyderabad.", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/Gpv6aHVU3rIRGv5vvT2SCkPJYB5gtEdhKnn3yBf_Gurmz87uUlN_kEGISoGCFriDm21ZFRFCSqGPH2fsuXACjTbipCk=s150", "url": "https://www.koolbusiness.com/newvi/5561960915533824.html"} , { "title": "Improve Memory and Brain Power with Branole X", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/TX3EGNSULaNOJWsxgvOXP6JHne24nnYFgWEcrHrNLKgb5OKFaH6WoI6853CQDyOgRYDFMF9VCSmLAp1rfE2T4gjduw=s150", "url": "https://www.koolbusiness.com/newvi/6742589980016640.html"} , { "title": "Aqua Grand +water purifier For Best Price in Megashope", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/mXzD6eG-xMr_cGN9GCinVjBCw_MdwfoY0clQ9vS-MEjJWumg4IhdZRSxhxTh17r1MJTalwRikyAX3HOCz_5DUyolFQ=s150", "url": "https://www.koolbusiness.com/newvi/5527058937544704.html"} , { "title": "Dell Honored at 2016 International Design Excellence Awards", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/ftXusdPY_UZlDxRFv3xq-sc_DPDO9pCG5GwhJGeAsC9fUqTi2Grz9RNlWwY4xel5NHoOcAOURLNOQBuCe-gJCvgO=s150", "url": "https://www.koolbusiness.com/newvi/5592851125633024.html"} , { "title": "Dell Honored at 2016 International Design Excellence Awards", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/D5kF6KJ0YXMB-kFYropQdRbNusngKg1DjEvKheo_aPrd1wn1vqRjvRfMC2Up0S8KHDlIsFoSKBuvC8VHj5ktUEqCrZA=s150", "url": "https://www.koolbusiness.com/newvi/5035817758097408.html"} , { "title": "Dell Honored at 2016 International Design Excellence Awards", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/TV2YoFBoQA3ATreDpqibPf_rps7H__MA-ctgZHtOJoIUMjo6sh8IIDmkaSkakjWIwhoR4EpIj-say4JXmXsVgMs-2vs=s150", "url": "https://www.koolbusiness.com/newvi/5556571268448256.html"} , { "title": "Buy gold necklace online at Amethystbyrahulpopli", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/kabOt5lt7EoRVxU-YkAvw8J4uLpoOfjFOXplJjQUYmxR_Jj4liL59fSQub3qNNFxXl-A4P45PglKHWcFrC0V3zh2lQ=s150", "url": "https://www.koolbusiness.com/newvi/6155801079054336.html"} , { "title": "Buy MLM software & Build MLM business online ", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/464yjWWhP16YDMJzzn_JN3GCW0KEO1xaOMAtLDEUzAhfX_xPNxu5qZIWPg8vQ81LpVluLzfNPANKLXYLCQxCG_Hhug=s150", "url": "https://www.koolbusiness.com/newvi/5363524836524032.html"} , { "title": "Dell Honored at 2016 International Design Excellence Awards", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/4nQU7IEgS4-5D_MDkxOzKR_76acQYY1PhWIEaJpCzbc3BgD7V7f1AVDeT6w5suXQOvmfbXobcqV7IMNSU9WfxHiy9rw=s150", "url": "https://www.koolbusiness.com/newvi/5518504436432896.html"} , { "title": "Famous Vastu Consultant Services in Gurgaon", "releaseYear": "2014", "url": "https://www.koolbusiness.com/newvi/6151819375935488.html"} , { "title": "Fully furnished Boys PG in sohna road gurgaon", "releaseYear": "2014", "url": "https://www.koolbusiness.com/newvi/6430480075325440.html"} , { "title": "Dell Honored at 2016 International Design Excellence Awards", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/Hl7GSSCQQJA_KY-33FB6uqVvOqgNPj8AKdXAj0jsJaFSlo2_6KPvJfMY2NKUUu6MkmhIeZm384EwOBT3Y4Y6qfGqz0w=s150", "url": "https://www.koolbusiness.com/newvi/5616690073174016.html"} , { "title": "Renting Leasing Residential Property in Sohna Road Gurgaon", "releaseYear": "2014", "url": "https://www.koolbusiness.com/newvi/6124910868955136.html"} , { "title": "Shivagiri Township Phase I., International SchoolSingle plot - (60 x 40) Rs.2400 Sq Ft", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/5Yf1wdA8FwjUQJCxFUarVxVqQEBVw1X-rfzOccwiWsxHDFdFSs4NgBhYBOnDEjKwlR7boQS6Qe1U6DC4cXCSYD7l6g=s150", "url": "https://www.koolbusiness.com/newvi/6351520859684864.html"} , { "title": "Buy Fully Furnished Commercial Property in Gurgaon", "releaseYear": "2014", "url": "https://www.koolbusiness.com/newvi/5025919469092864.html"} , { "title": "Dell Honored at 2016 International Design Excellence Awards", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/t1KEzWjVqxYFQAiLkxmOl1xveiyUeQ8MhrAuH_KYodsUwtidNha7T0LyzdzIkD70FKo2mGHyHuPwWd35BYbGpIv_Pso=s150", "url": "https://www.koolbusiness.com/newvi/4912444554084352.html"} , { "title": "Aqua Grand +water purifier For Best Price in Megashope", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/H9QBDOXFyN6jvLF-7qg9Mm6bfvgHccfWt6VGs53oq-6Lynh-7EFAycGC9ysanmqZww4OPfNo3gFhfjjgT6WcH_YexKA=s150", "url": "https://www.koolbusiness.com/newvi/6186115495100416.html"} , { "title": "ANSYS COURSES IN CHENNAI | BEST ANSYS TRAINING - 9884433249", "releaseYear": "2014", "url": "https://www.koolbusiness.com/newvi/5609388628770816.html"} , { "title": "AutoCAD Courses for Civil Engineers in Chennai | Civil AutoCAD Training", "releaseYear": "2014", "url": "https://www.koolbusiness.com/newvi/6120900711677952.html"} , { "title": "AutoCAD Training Institute in Chennai | Best AutoCAD Institute", "releaseYear": "2014", "url": "https://www.koolbusiness.com/newvi/6319819437637632.html"} , { "title": "Aqua Grand +water purifier For Best Price in Megashope", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/dBEZi48stLzoSa935dSwrG6XG9rHWauVibD6hL-qNMyPSyl9NQ3r-RDL31__Y1z5x0J9ypv2406WA5EWlQN15_yQcw=s150", "url": "https://www.koolbusiness.com/newvi/5028809445212160.html"} , { "title": "SEBS PROVIDING HOME BASED WORKS ..", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/oz6lXftn2ZAo72l94ZIZDt8k5zma8Qi_Sj590HD0arbqlZYw5Q9TbHGeDMaOw4DPC4wXj3sgV354Uq_q1ceOFiI9Ag=s150", "url": "https://www.koolbusiness.com/newvi/5042552803688448.html"} , { "title": "India's most successful online examination software - Tutoreal", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/OU9YwSGw0ipdKrmhbrvj4zgQf1zB_q4vHQExrkq3Qhlz1Em29QkpFLEIQGuRtW5R34eU06vfEMXMBMzlgIb3LmpgYw=s150", "url": "https://www.koolbusiness.com/newvi/6090008890966016.html"} , { "title": "DO THE PART TIME EARN REGULARLY", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/eOPNfb9X0Bww0hWgmkDRWCMDVF1UbD6FxC7dUa7vObSakSe-0ry6_1gzfBqlhqohbePu-G3YQCuNEjcG9O2oiJA_=s150", "url": "https://www.koolbusiness.com/newvi/6449927687241728.html"} , { "title": "WHO WANT EXTRA INCOME DO PART TIME JOB", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/rAk2Tigz2eq9ObmQial0dGqhunIDmoOslLo-mWjtoY_XCX8Zdt3WLGLDH4N73dTstCiuGKGRhPzmTYtFOeuP2jXkyn4=s150", "url": "https://www.koolbusiness.com/newvi/5756869484216320.html"} , { "title": "FEELING BOAR THEN FILLUP THE FORMS", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/qg58D3wCeFW6dGqlbIwocJ21HT3-1jv1Yu8mKBPLLAgNo0w9Zuo1ddRvbVnXkCrHG5takW6Qu90JG44axHByiF05=s150", "url": "https://www.koolbusiness.com/newvi/6081454389854208.html"} , { "title": "ONCE REGISTER IN SEBS TAKE ONE YEAR INCOME", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/LFUJB7ftdt01IcURnDPp1sp11luU_I16lETvCZJy86ndhEPw3t8YUBuf8Mwylktj3RucJX62vlTrMJZME23cOfQMJg=s150", "url": "https://www.koolbusiness.com/newvi/5029901172211712.html"} , { "title": "Pay Per Click (PPC) Services Company Delhi & Across India", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/nkfVvC8BWZaOCQKKQxhWngEXX1zUTQVhgfWLQnGoFIn3JD7xg5CmdrZ0JFmC8PxUm10l-hZ1dBRGYwvMjfcO_QllRQ=s150", "url": "https://www.koolbusiness.com/newvi/5046438675349504.html"} , { "title": "SEBS PROVIDING WORKS FOR STUDENTS", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/pS_UVwB-a_GUMBqEHOlyX4oubrbCrDOBz41KW_DWkMGSyFcbjAtVr9hbO_rg4YhE-XN5wTAVF7yCLBgLWL5J-vJWS2o=s150", "url": "https://www.koolbusiness.com/newvi/6334093358792704.html"} , { "title": "PART TIME HOME BASED WORKS ARE AVAILABLE ", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/DxEGyB6jwYQNIC95qcTa1Ty_mR8UOt6ac9hI3SiSxALUe-2pxsZpZMyu2E4SQKYQrDREP1T-tPGie3C2-fmwKqSM5Gw=s150", "url": "https://www.koolbusiness.com/newvi/6360722558681088.html"} , { "title": "JUST FILLUP THE FORMS WE WILL PROVIDE MONEY", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/4afOgWiVS9Mp1CqaBWRKOJciIeKtlx6BcZtje6olRNTeTrIXGLYsa0_UURL9MNjurD1txcJJFmKaWmQVQRB4G7kl3q8=s150", "url": "https://www.koolbusiness.com/newvi/5335997048946688.html"} , { "title": "NOW U GET WEEKLY PAYMENT THROUGH LAPTAP", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/l8lSMOQAlwFjaFfGJYlHHxZauN3FNyKoKWCR_15JEqtZFdU0Fl-acBmNovThWdxaCvByeNxYTdEMs6RSsVlYqUNTQwU=s150", "url": "https://www.koolbusiness.com/newvi/5271450132938752.html"} , { "title": "HURRY UP VACANCIES ARE START", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/rE5RiFhsinGtWeBEnXhB-txkj5A5SBPzaQ1gOdjozreykKxo4D367WXvYItUV5tfzuq3IoregaRLahk3OO0OJqXw6rU=s150", "url": "https://www.koolbusiness.com/newvi/5208193451950080.html"} , { "title": "web designing company in visakhapatnam ", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/7HmV0NElSz8vHAtJmN-FUJF0PqzYtey-Yu9KzAxOWExsNsmZYmYaohfl8UbsRrBj0VfP8ga0VfX9vBqycPTiVtMG9sw=s150", "url": "https://www.koolbusiness.com/newvi/4964108984123392.html"} , { "title": "CALORIZED OXYGEN LANCING PIPES", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/AZTmI7wTK9i7IdeGLNefH312nJPjgSeMg7YLXg9ikmXc1_IpmJyJWf6vR0BKhYplYXwHqY9Y0TmZj9tOOmlzl8XWjw=s150", "url": "https://www.koolbusiness.com/newvi/5063230118428672.html"} , { "title": "Best restaurants in Delhi , Satya Niketan", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/FwCr3e3EaikXod8Wp6PELnISvU92_ArD79YqRt_q3yb8YjZmwXtqS5B884sCMJaPKi_ma3m7uNuVbbrAUxVgNXbaYg=s150", "url": "https://www.koolbusiness.com/newvi/5225620952842240.html"} , { "title": "Artificial Lawn Grass Manufacturer and Supplier in delhi- Dspaze", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/hDtSO48SIFa_IEojY7V1rc8f3JHjh7iH6GmrLhRH2SNfX6vgrYUk8IMFuuoJDTajyIcxGBoxccprIvZojuEkqULX_g=s150", "url": "https://www.koolbusiness.com/newvi/6114783336071168.html"} , { "title": "Big data training in bangalore", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/vaV5q_cW9G6V850gtf4j9OMxMFBn2wXi4b-h4O0sJib_-fNpChVi2rnN2qJAEsMkXKsNOhpW-Fty0hchLptr9wSNR48=s150", "url": "https://www.koolbusiness.com/newvi/5313711805825024.html"} , { "title": "Contact Verdure - Best Swimming Pool Vendor in Delhi ", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/JCbNplL03XfL-r7DAiNdmN9YUM0KbhpCxtxijieq2xe57C2lAznwCZWozD6277nFM1yuUkVjKa-cPeXpGfp5OhvebQ=s150", "url": "https://www.koolbusiness.com/newvi/6364646816612352.html"} , { "title": "Aqua Grand +water purifier For Best Price in Megashope", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/Qw_cEOu8ECK4RBXlRhCCc_Pa0ks0tI8EOp-8xFsCpKx4RJYY1_7XXDc3HqV1c2PfN_hHrie8AjbkwqNzMsreA_PRtA=s150", "url": "https://www.koolbusiness.com/newvi/5876661759246336.html"} , { "title": "Scanner Tenders, Tenders By Scanner, Tenders For Scanner", "releaseYear": "2014", "url": "https://www.koolbusiness.com/newvi/5238746909769728.html"} , { "title": "Dr. Arun Kumar Singh - Best Endocrinologist in Faridabad, NCR", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/8mdHMf2xTufAPasbX6ButMBRTBQf4WvnU6bUAquxLcKfLoHykrQ88LH5niPoIPqdKiicZXnELCS90nVZDa_jxeCX=s150", "url": "https://www.koolbusiness.com/newvi/5286396216475648.html"} , { "title": "Wholesale Women Clothing Suppliers", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/lD5xaOsaBAT4xchvXzQNHm81wnNdTG-M-w8iq2djhRRp4CZH8NyOxjz9XWlxUwB2N7zf7onYLVlsN0f9eSUD7ZT85A=s150", "url": "https://www.koolbusiness.com/newvi/5857946976124928.html"} , { "title": "Earn Money Online RS 24000/Month F/P/T", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/AdK3MLruGvx66rRO7567EZivvSOI8I-6RDrS_Bo59Az6-PhRHzu1YW6ECYDzdcsT374W-xCEPIy2gJx-xuSV1LtTAQ=s150", "url": "https://www.koolbusiness.com/newvi/5053740119752704.html"} , { "title": "Job problem solution in Mumbai", "releaseYear": "2014", "url": "https://www.koolbusiness.com/newvi/4977441099481088.html"} , { "title": "Best HR Policy Classes in Delhi NCR", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/ONkJrdtozkk6E1_0ZFjNubFCJUhr2tGEd0HaC1U8ZwCdVemPOQfeDWPPih6QJogRwPQo3PfVMHvFg9G6wnNJgQeXpQ=s150", "url": "https://www.koolbusiness.com/newvi/5224881413160960.html"} , { "title": "A Dedicated Web Portal/Website Of Solan District.", "releaseYear": "2014", "img": "https://lh3.googleusercontent.com/lLSF35XwnQ7ld4cBuTSs3lZdeMSG8PyKGb6qTHFcqBlGH0NxOxI9K8MqjT9vZ3vYeT0Lr9N3akcEWfvVaGFpzl9quQ=s150", "url": "https://www.koolbusiness.com/newvi/6343054942273536.html"} ] }
\$\endgroup\$
1
\$\begingroup\$

Instead of having the .json output being served from a template file, make use of the json module function of webapp2_extras.

The current response headers for the /in.json page has:

Content-Type: text/html; charset=utf-8

whereas, it should be ideally: application/json.


Why you should avoid creating a json in text is because if you later want to add/remove a key, or iterate over a creating a certain key, you might run into syntax issues. Debugging those syntax errors would be a pain since you run 3 level deep in the existing code already.

Instead, create a python dict in the handler code itself, and send a response as follows:

from webapp2_extras import json
...
self.response.headers['Content-Type'] = 'application/json'
self.response.write(json.encode(your_python_dict))

This way, a client will not receive a malformed JSON response ever, and the server would simply issue a 5xx error code.

\$\endgroup\$
  • \$\begingroup\$ It works your way. It was even easier. \$\endgroup\$ – Niklas Rosencrantz Mar 18 '18 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.