Search code examples
pythonjsonescapingmarkup

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":"<ul><li class="lists">Hello World</li><ul>"
    }
}

is this possible to do in Python without requiring to to be escaped beforehand?

It will be a string initially so I was thinking about writing a regular expression to attempt to match and escape these prior to processing, but I just want to make sure there isn't an easier way.


Solution

  • Well, depending on how varied your HTML is, you can use single quotes in HTML fine, so you could do:

    {
        "node":
        {
            "list": "<ul><li class='lists'>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><li class="lists">Hello World</li><ul>'}})
    '{"node": {"list": "<ul><li class=\\"lists\\">Hello World</li><ul>"}}'