Search code examples
meteormeteor-blaze

Meteor/Blaze Print Object as JSON


All,

I have on object in Meteor/Blaze. I tried this.

{{data}}

And it output

[object Object]

Is there any way I can get it to output JSON instead?


Solution

  • If you're looking to print a JSON object in JSON format in your Blaze view, you might wanna look at JSON.stringify() method.

    Home.js [Helper example]

    import './Home.html';
    
    Template.home.helpers({
      jsonPrint(jsonObject) { // with Latest Javascript ECMAScript 2015+
        return JSON.stringify(jsonObject);
      }
    })
    

    Home.html [Your Blaze view]

    <template name="home">
        <body>
            <p>
              JSON output:
            </p>
            <div class="code">
              {{jsonPrint yourJsonObject}}
            </div>
        </body>
    </template>
    

    Reference: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify