Search code examples
pythonjsonpyramidpython-2.6mako

" appears in JSONEncoder output, input is Python list of strings


I'm reading a text file, splitting it on \n and putting the results in a Python list.

I'm then using JSONEncoder().encode(mylist), but the result throws errors as it produces the javascript:

var jslist = ["List item 1", "List item 2"]

I'm guessing switching to single quotes would solve this, but it's unclear how to force JSONEncoder/python to use one or the other.

Update: The context is a pyramid application, here's the end of the function (components is the name of the list:

return {'components': JSONEncoder().encode(components)}

and then in the mako template:

var components = ${components};

which is being replaced as above.


Solution

  • mako is escaping your strings because it's a sane default for most purposes. You can turn off the escaping on a case-by-case basis:

    ${components | n}