6

I tried json filter, but I get "json" filter not found error. What I'm I doing wrong?

Error: Filter "json" not found at Object.exports.wrapFilter (......./node_modules/swig/lib/helpers.js:310:11)
<script type="text/javascript">
        {{ places|json }}
</script>

object is passed from mongodb

this.displayMainPage = function(req, res, next) {
    "use strict";

    places.getPlaces(10, function(err, results) {
        "use strict";

        if (err) return next(err);

        return res.render('places_template', {
            places: results
        });
    });
}

Edit: I'm trying to output json to pass to google maps and display the same data in html

Vlad Vinnikov
  • 1,457
  • 3
  • 22
  • 33

1 Answers1

11

For versions of Swig prior to 1.0, the json filter was instead named json_encode:

{{ places|json_encode }}

And, for compatibility, 1.0 and later keep json_encode as an alias of json.


Though, you should also consider upgrading Swig:

$ npm install swig@0.14
npm WARN deprecated swig@0.14.0: v1.0.0 is a complete rewrite of Swig
from the ground up. Previous versions are no longer supported
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • thank you, upgrading swig fixed the error, but I get json with &quot. ` [{"_id":"52895b73ab025fe9ce4b980c","address1":"Boerum Pl. & Schermenhorn Street","address2":"","city":"Brooklyn","lat":40.6903327,"lng":-73.9896449,` How do I remove them? – Vlad Vinnikov Dec 08 '13 at 01:46
  • 2
    @VladVinnikov You can use [`{% autoescape false %}`](http://paularmstrong.github.io/swig/docs/tags/#autoescape) to disable HTML encoding. – Jonathan Lonowski Dec 08 '13 at 01:55
  • 11
    I prefer to not disable autoescaping for all filters, only for this case you can use filter "raw": `{{ places|raw|json_encode }}` – Krayton Mar 04 '14 at 18:21
  • 1
    `|raw` should be part of the main answer. Thanks @Krayton – Fydo Jul 10 '16 at 16:37