Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use User JSON file in Meteor?

I need to know the JSON file usage in Meteor JS. First where the JSON file store in Meteor Folder Structure and How to get JSON Data using Meteor JS and is it JSON file save using the extension .JSON?. I didn't get any idea about this. Can you please give me suggestions about JSON files or is there any best references for understanding JSON files in Meteor.

like image 375
Venkat Avatar asked Feb 25 '14 04:02

Venkat


People also ask

Can you put Boolean in JSON?

JSON BooleansValues in JSON can be true/false.


1 Answers

If you want to read JSON data, you can use the assets api. You can do the following test in any project:

1) Create a file called private/test.json with the following contents:

[{"id":1,"text":"foo"},{"id":2,"text":"bar"}]

2) Read the file contents when the server starts (server/start.js):

Meteor.startup(function() {
  console.log(JSON.parse(Assets.getText('test.json')));
});

Here we are using getText to read the contents of the file (it assumes the file is located in the private directory). Then we are passing the JSON string contents to parse which will return an object.

Note that the file extension (.json) does not matter, however it is conventional to use it.

like image 167
David Weldon Avatar answered Oct 12 '22 05:10

David Weldon