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.
Asked
Active
Viewed 5,351 times
1 Answers
11
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.

David Weldon
- 63,632
- 11
- 148
- 146
-
1yeah,But how to read `text` only as you told above example.Can you please give me suggestion?.@David Weldon – Venkat Feb 25 '14 at 07:45
-
The `getText` function just reads text-based files. So if you want to read a file containing only text, just remove the call to `JSON.parse`. – David Weldon Feb 25 '14 at 07:51
-
Okay,But is not able to get data using reference of parse object?.@David Weldon. – Venkat Feb 25 '14 at 08:06
-
I'm sorry, I want to help but I don't understand what you are asking. :( – David Weldon Feb 25 '14 at 08:13
-
Means How to Iterate Data in Json file.@David Weldon. – Venkat Feb 25 '14 at 08:23
-
If you do call `JSON.parse` then you have a regular javascript object which you can iterate however you like (using `_.each` for example). Just try using the code above - I'm sure you'll figure it out. – David Weldon Feb 25 '14 at 08:28
-
The docs say that assets have to be static. Is there some way for me to load new files and get its text while the app is running? – FullStack Mar 12 '15 at 12:40
-
1@FullStack That's really a separate question. You can't use `Assets` in this case - you'd need to save the file elsewhere on the disk and use `fs` to read it. – David Weldon Mar 12 '15 at 22:59