librelist archives

« back to archive

Ignoring HTML tags in JSON files

Ignoring HTML tags in JSON files

From:
Samrat Man Singh
Date:
2011-09-06 @ 16:11
I need to display a JSON file so I wrote this code:

@app.route('/json')
def showJson():
   filename = open('myfile.json','r')
   return filename.read()

But the JSON file also contains HTML tags like <img src='imagefile.jpeg'/>,
so when I run the app, the image shows up instead of the JSON content. How
can I solve this error? Is there a more elegant solution than just escaping
the HTML?

Samrat

Re: [flask] Ignoring HTML tags in JSON files

From:
Amirouche Boubekki
Date:
2011-09-06 @ 18:15
Hi,


> But the JSON file also contains HTML tags like <img src='imagefile.jpeg'/>,
> so when I run the app, the image shows up instead of the JSON content. How
> can I solve this error? Is there a more elegant solution than just escaping
> the HTML?
>

http://pypi.python.org/pypi/bleach/ is a smart html stripper

Regards,

Amirouche

Re: [flask] Ignoring HTML tags in JSON files

From:
Simon Sapin
Date:
2011-09-06 @ 17:02
Le 06/09/2011 18:11, Samrat Man Singh a écrit :
> I need to display a JSON file so I wrote this code:
>
> @app.route('/json')
> def showJson():
>    filename = open('myfile.json','r')
>    return filename.read()
>
> But the JSON file also contains HTML tags like <img 
> src='imagefile.jpeg'/>, so when I run the app, the image shows up 
> instead of the JSON content. How can I solve this error? Is there a 
> more elegant solution than just escaping the HTML?
>
> Samrat
>

Hi,

If you want your whole HTTP response to be JSON instead of HTML, you 
need to say it explicitly:

     from flask import make_response

     @app.route('/json')
     def show_json():
         with open('myfile.json') as file:
             response = make_response(file.read())
         response.headers['Content-Type'] = 'application/json'
         return response

Otherwise Flask uses the default content type: text/html. If you want to 
include a chuck of JSON inside an HTML response, then templates with 
escaping are the right thing to do.

Regards,
-- 
Simon Sapin

Re: [flask] Ignoring HTML tags in JSON files

From:
Armin Ronacher
Date:
2011-09-06 @ 21:40
Hi,

On 2011-09-06 7:02 PM, Simon Sapin wrote:
> If you want your whole HTTP response to be JSON instead of HTML, you
> need to say it explicitly:
For this common case you can also implement it like this:

@app.route('/json')
def show_json():
     with open('myfile.json') as f:
         return f.read(), 200, {'Content-Type': 'application/json'}

http://flask.pocoo.org/docs/quickstart/#about-responses


Regards,
Armin

Re: [flask] Ignoring HTML tags in JSON files

From:
Samrat Man Singh
Date:
2011-09-06 @ 17:12
Thanks, Simon. That's definitely more like what I had wanted.

Samrat

On Tue, Sep 6, 2011 at 10:47 PM, Simon Sapin <simon.sapin@exyr.org> wrote:

> **
> Le 06/09/2011 18:11, Samrat Man Singh a écrit :
>
> I need to display a JSON file so I wrote this code:
>
> @app.route('/json')
> def showJson():
>    filename = open('myfile.json','r')
>    return filename.read()
>
>  But the JSON file also contains HTML tags like <img
> src='imagefile.jpeg'/>, so when I run the app, the image shows up instead of
> the JSON content. How can I solve this error? Is there a more elegant
> solution than just escaping the HTML?
>
>  Samrat
>
>
> Hi,
>
> If you want your whole HTTP response to be JSON instead of HTML, you need
> to say it explicitly:
>
>     from flask import make_response
>
>     @app.route('/json')
>     def show_json():
>         with open('myfile.json') as file:
>             response = make_response(file.read())
>         response.headers['Content-Type'] = 'application/json'
>         return response
>
> Otherwise Flask uses the default content type: text/html. If you want to
> include a chuck of JSON inside an HTML response, then templates with
> escaping are the right thing to do.
>
> Regards,
> --
> Simon Sapin
>

Re: [flask] Ignoring HTML tags in JSON files

From:
Adam Oakman
Date:
2011-09-06 @ 16:19
I assume you are outputting this on a template, per chance, do you  
have it marked as SAFE? Have you tried using the escape function? 
http://jinja.pocoo.org/docs/templates/


On Sep 6, 2011, at 11:11 AM, Samrat Man Singh wrote:

> I need to display a JSON file so I wrote this code:
>
> @app.route('/json')
> def showJson():
>    filename = open('myfile.json','r')
>    return filename.read()
>
> But the JSON file also contains HTML tags like <img  
> src='imagefile.jpeg'/>, so when I run the app, the image shows up  
> instead of the JSON content. How can I solve this error? Is there a  
> more elegant solution than just escaping the HTML?
>
> Samrat
>

Re: [flask] Ignoring HTML tags in JSON files

From:
Samrat Man Singh
Date:
2011-09-06 @ 16:52
Thanks. I wasn't outputting on a template initially but I see that solves
the problem :)

Samrat

On Tue, Sep 6, 2011 at 10:04 PM, Adam Oakman <adam.oakman@gmail.com> wrote:

> I assume you are outputting this on a template, per chance, do you have it
> marked as SAFE? Have you tried using the escape function?
> http://jinja.pocoo.org/docs/templates/
>
>
> On Sep 6, 2011, at 11:11 AM, Samrat Man Singh wrote:
>
> I need to display a JSON file so I wrote this code:
>
> @app.route('/json')
> def showJson():
>    filename = open('myfile.json','r')
>    return filename.read()
>
> But the JSON file also contains HTML tags like <img src='imagefile.jpeg'/>,
> so when I run the app, the image shows up instead of the JSON content. How
> can I solve this error? Is there a more elegant solution than just escaping
> the HTML?
>
> Samrat
>
>
>