37

I have a data.json file that I would like to load and that I have placed in the lib/ folder. What should I do in order to load that JSON into a variable in the server? Thanks

Alex Gonzalez
  • 981
  • 1
  • 11
  • 17

3 Answers3

80

There are three ways you can go about this, it depends what you're most comfortable with & your use case.

The first is to store it as a JS Object

if your json data is { "name":"bob" } you could use myjson = {"name":"bob"} in a .js file in the /lib folder and just call myjson when you need it.

Using an http call

You need the Meteor http package, installed via meteor add http.

Server Side code

myobject = HTTP.get(Meteor.absoluteUrl("/myfile.json")).data;

Client Side Code

HTTP.get(Meteor.absoluteUrl("/myfile.json"), function(err,result) }
    console.log(result.data);
});

Another way to do it is to fetch the json file ajax style (you would have to put it in your /public folder though and use Meteor.http to call it.

Read the file directly

Lastly you could read the file directly, you store your myfile.json in a private directory in your project's root:

var myjson = {};
myjson = JSON.parse(Assets.getText("myfile.json"));

If you want to access this on the client side you would have to interface it with a Meteor.methods and Meteor.call

So whichever way you want, the first is the easiest but I'm not too sure how you want to use it or whether you want to pick the file or something

Tarang
  • 75,157
  • 39
  • 215
  • 276
  • I ended up using the first solution as it is the easiest. I added the file to /server so it does not load on the client as well. Thanks – Alex Gonzalez Mar 14 '13 at 00:01
  • Hi Akshat I tried your code but i am getting "ReferenceError: __meteor_bootstrap is not defined". my json file is in the same folder as the js file. – Rishav Sharan Apr 02 '13 at 11:28
  • Is this client side code? The above will only work in the `if(Meteor.isServer) {..}` block or anywhere server side js is run such as in the `/server` folder. npm modules can't be included in on the client so instead you can fetch it with the http call – Tarang Apr 02 '13 at 11:30
  • I have uploaded the code to my github. its the leaderboard example. https://github.com/StudioMockingbird/SH_meteor – Rishav Sharan Apr 02 '13 at 11:35
  • sorry about that @RishavSharan me to blame there I typod its actually `__meteor_bootstrap__` not `__meteor_bootstrap` – Tarang Apr 02 '13 at 11:38
  • You can also [pass the JSON file to the `--settings` parameter to `meteor`](http://docs.meteor.com/#/full/meteor_settings). – Dan Dascalescu Nov 25 '14 at 05:40
  • Hey @Akshat why do you differentiate http calls as Server Side and Client Side? I can't figure out how to use the Server Side snippet. – n370 Aug 04 '15 at 04:17
  • @n370 The client doesn't support fibers, meaning it would return `undefined` on the client. – Tarang Aug 04 '15 at 04:27
  • @Akshat I'm sorry for being dumb but here is a [gist](https://gist.github.com/n370/f244ed654714a8146502) with a little bit more information about the issue I'm going through. I could go whatever way, my only restriction is to maintain the folder structure as shown and have a variable with the JSON data available both in the client and the server. – n370 Aug 04 '15 at 13:59
  • Why not `var myjson = JSON.parse(Assets.getText("myfile.json"));`? – Erdal G. Jan 23 '16 at 20:43
  • Re: first solution, make sure not to initialize the variable with a `var` to make it a global variable, for more details- https://stackoverflow.com/questions/26836390/how-can-i-access-constants-in-the-lib-constants-js-file-in-meteor?lq=1 – aturc Feb 19 '18 at 18:39
2

As I am new to all this I suspect this is not the correct way to do this, however this has worked for me...

Three coffee script files, two in the server directory:

server.coffee:

Meteor.startup ->
    insertSample = (jsondata) ->
      Fiber(->
        Documents.insert
          name: "Sample doc"
          data: jsondata
      ).run()        
    if Documents.find().count() is 0
      insertJSONfile("tests/test.json", insertSample)

and insertJSONfile.coffee:

fs = __meteor_bootstrap__.require("fs")

insertJSONfile = (file, insert) ->
  jsondata = undefined
  fs.readFile file, (err, data) ->
    throw err  if err
    jsondata = JSON.stringify(JSON.parse(data))
    insert(jsondata)

and model.coffee in the root dir:

@Documents = new Meteor.Collection("documents")

On startup this should load and insert the JSON file (in my case I've stored this in the tests directory) into a field in the documents collection.

I would love to hear from others on how this should be done properly.

Paul Young
  • 401
  • 3
  • 10
1

I assume you want the json content to be represented as an object and not as a simple string.

I use js-yaml (https://github.com/nodeca/js-yaml), assuming you install the npm package. You can also just copy it manually.

yaml = __meteor_bootstrap__.require('js-yaml')
fs = __meteor_bootstrap__.require('fs')
content = fs.readFileSync(file, 'utf8')
object = yaml.load(content)

and that's it! I personally persist my json into meteor collections.

Thierry
  • 339
  • 1
  • 8
  • I prefer using JSON as I already have the file in that format. I am loading this file (which contains cities and regions) in memory at the start of the Meteor app. – Alex Gonzalez Mar 14 '13 at 00:02
  • Also if using yaml have a look at the yaml package on atmosphere, its already built for meteor – Tarang Mar 14 '13 at 00:09