webpy: How to serve JSON

Giovanni Di Milia picture Giovanni Di Milia · Aug 18, 2010 · Viewed 20.1k times · Source

Is it possible to use webpy to serve JSON? I built my website and I need to serve some information in JSON to interact with the Javascript on some pages.

I try to look for answers in the documentation, but I'm not able to find anything.

Thanks, Giovanni

Answer

Mark picture Mark · Aug 18, 2010

I wouldn't think you'd have to do any thing overly "special" for web.py to serve JSON.

import web
import json

class index:
    def GET(self):
        pyDict = {'one':1,'two':2}
        web.header('Content-Type', 'application/json')
        return json.dumps(pyDict)