Dan Grahn
Dan Grahn

Reputation: 9424

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?

Upvotes: 2

Views: 1199

Answers (3)

robertdavid
robertdavid

Reputation: 403

I actually use a dev helper defined as below and which is in my own prototyping boilerplate:

Template.registerHelper("toJSON", function (object) {
  return object ? JSON.stringify(object, null, 2) : null;
});

Upvotes: 0

Afif Sohaili
Afif Sohaili

Reputation: 246

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

Upvotes: 2

Keevan Dance
Keevan Dance

Reputation: 68

If you are trying to return values in that object, you will need to use JS dot notation so

{{foo.bar}}

where

Upvotes: -1

Related Questions