Skip to content Skip to sidebar Skip to footer

Html Tags Within Json (in Python)

I understand its not a desirable circumstance, however if I NEEDED to have some kind of HTML within JSON tags, e.g.: { 'node': { 'list':'
  • >Hello World</li><ul>" } }

    However, with simplejson, which is built into Python 2.6 as the json module, it does any escaping you need automatically:

    >>> import simplejson
    >>> simplejson.dumps({'node': {'list': '<ul><liclass="lists">Hello World</li><ul>'}})
    '{"node": {"list": "<ul><liclass=\\"lists\\">Hello World</li><ul>"}}'
    

Solution 2:

You can have arbitrary strings there, including ones which happen to contain HTML tags (the only issue with your example is the inner " which would confuse any parser).

Post a Comment for "Html Tags Within Json (in Python)"