1

This is my first time working with JSON and I'm still fairly new to Javascript. I am working on an app and I will be needing to work with several sets of data. Since there are nearly 300 entries with 6 properties each I plan to store them in a JSON format. In the app I will need to retrieve the data and do several calculations with it.

I am having some trouble deciding how to store the data. I know that it is generally bad practice to store things as global variables even though it is convenient and easy to work with. Because of this I was considering storing the data in a separate file and loading it in using jQuery. This method makes it much more tedious to work with the data, but it has the benefit of making it very easy to update the entries when they become out of date. I'm also unsure how much this method would affect load times.

I'm not sure which option is preferable or if there are better options. How would you do it?

Nathan
  • 956
  • 6
  • 6
  • Not sure what you’re asking. If you have JSON in a separate file you can only load it via a server request (e.g. AJAX in jQuery). Global variables don’t play a role there. If you have something like `var myJSON=` at the top of your JSON file, it’s not a JSON file anymore, at least not a valid one… most of the time JSON is loaded via AJAX, although [some sites prepend a `while(1);` or something similar to it for security reasons](http://stackoverflow.com/questions/2669690). – Sebastian Simon Sep 13 '15 at 00:26

3 Answers3

2

Sounds like you need to access and modify the data in your JSON. in order to do you will need to develop a logic in JavaScript that will help deserialize/serialize to manage your objects.

As for the location , you can store it in a file locally of you can use a database table if you have access to one and create a table with a 'JSON' column.

You have a lot of possibilities you just need to be specific on how you want to use it.

IndieTech Solutions
  • 2,429
  • 1
  • 18
  • 31
2

Apart from the location of your data you can avoid using global objects if you put all your code inside a module, take a look at amd or asynchronoud module definition it's a great mechanism.

Nos, if we come back to the data part, I hhe best way is to store your data in a json file and write a function inside your module that can handle loading data and even editing it.

cнŝdk
  • 28,676
  • 7
  • 47
  • 67
2

Personally, I'd put data in .json file and load it using ajax.

It doesn't make data tedious to work with at all. It's STILL json and be converted to Javascript Object.

Example:

http://jsfiddle.net/mon3419m/

$.get('https://www.reddit.com/.json').done(function(data){
    data.data.children.forEach(makeLi);
});

and as for global problem..

just put your code like this:

(function(){

     //all your code.

})();

Global Code is a problem when your app is big or you have scripts running from other sources along yours. Which can do something to your variables unintentionally. But using this self invoking function you can prevent all that. Because function in js create new scope. Putting (function(){})(); is like calling function and telling it to execute right away.

to better understand look at this..

funcName(); //invokes funcName
(funcName)(); //invokes funcName
(function funcName() {})(); //invokes funcName
(function(){})(); //invokes the nameless function
Muhammad Umer
  • 14,722
  • 14
  • 69
  • 139