I have a Python script that builds some JavaScript to send down to the browser in a JSON envelope. I'd like to escape the JavaScript strings and delimit them with single quotes. I can't use json.dumps because it uses double quotes as delimiters like the JSON spec requires.
Is there a JavaScript String escape method in Python?
Example
def logIt(self, str):
#todo: need to escape str here
cmd = "console.log('%(text)s');" % { 'text': str}
json.dumps({ "script": cmd })
So logIt('example text') should return something like this:
{
"script": "console.log('example text');"
}
logIt('Uh oh\'')