Best way to handle JSON file?


#1

Hey, folks. I’m starting to work on a new Meteor project, and was wondering what the best way would be to incorporate this database of MTG cards (I’m writing a Meteor-driven deck building application for a school project):

http://mtgjson.com/ <-- This is the database in question, and they offer just a JSON file, as opposed to a REST-based API that I could just make queries to. I’m not sure if I should do something like try to shove the JSON into a collection that could be queried, or what the best way to handle the situation would be.

Any assistance/guidance is definitely appreciated in advance. Thanks.


#2

See @looshi poem, you can try an example with json and Data.find() and see if it works for you.

http://blaze.haus/browse/0/32


#3

I tend to dump the JSON file in the server/lib folder, rename the Json file to .js and then set the outermost array to a variable. You could also read the file from disk using NodeJS methods and the parse as JSON but that’s a few extra steps.


// both/lib/collections.js
this.db = {};
db.cards = new Mongo.Collection('cards');

// server/fixtures.js
Meteor.startup(function(){
  // if no cards are present insert the json
  if (db.cards.find().count() === 0) {

    // itterate through each object in json
    cardJson.forEach(function(card) {
      // insert into db
      db.cards.insert(card);
    });

  }

});


// server/lib/card_fixtures.js
cardJson = [{...},{...}, ...];


#4

This seems like as good a solution as any. I’ll give it a shot and see what comes of it. Thanks a lot for the quick response.