1

I have a .json file that is 50MB and 84 000 lines. I am using the code below to load it into the variable 2002data. At first I was waiting for a while as expected but when I added an alert before $.get nothing was coming up. Am I doing something wrong or is this normal?

alert('starting 2002_results.json'); // This alert isn't showing up...
$.get('2002_results.json', function(2002data));
alert('2002 done');

Just in case this makes a difference, here is the first 2 and last 2 lines of the .json file.

{"2002Results": [

{"st_is_str":"01006000102","parm_desc":"ALKALINITY, TOTAL"},

{"st_id":{"station_id":"18349002002","name":"Madawaska River","location":"Madawaska Blvd, Arnprior","first_year":1966,"last_year":2010,"total_years":45,"missed_years":0,"long_coord":-76.34968485,"lat_coord":-76.34968485},"parm_desc":"ZINC,  UNFILTERED TOTAL"},

]}

..and this goes on for a while.

If there is something more efficient I can use what is it? (Just a name would be good, I'll figure the rest out)

m59
  • 43,214
  • 14
  • 119
  • 136
user3210944
  • 57
  • 1
  • 6
  • `alert()`s will block anything that follows them. So, as long as the dialog with `'starting ...'` is showing, `$.get()` won't have actually started. Try [`console.log()`](https://developer.mozilla.org/en-US/docs/Web/API/console.log) instead of `alert()` for less invasive debugging. Also note that [`$.get()` is asynchronous](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call), so the result is only available within the callback `function` you give it. – Jonathan Lonowski Jan 19 '14 at 03:42
  • 2
    transferring 50MB of data should be a big no.... you should do some server side processing and sent back only the very necessary data to the client – Arun P Johny Jan 19 '14 at 03:46
  • @ArunPJohny I'm starting to realize that. lol – user3210944 Jan 19 '14 at 18:17

1 Answers1

3

Loading things:

You shouldn't load 50mb of data at once. That is a huge server load and too time consuming. Instead, evaluate what the user needs to see at the time and just load enough to handle that. Loading on scroll, loading on click, or pagination are common approaches.

Syntax errors!

You have a syntax error. Change your code to:

$.get('2002_results.json').then(function(resp) {
  alert('2002 done');
});

or:

$.get('2002_results.json', function(resp) {
  alert('2002 done');
});

You should check your console (probably f12 key). function(2002data)); is not valid javascript. If you have a syntax error, nothing in the script will run.

Also, your JSON is invalid. Here is the corrected JSON:

[
  {
    "2002Results": [
      {
        "st_is_str":"01006000102",
        "parm_desc":"ALKALINITY, TOTAL"
      },
      {
        "st_id":{
          "station_id":"18349002002",
          "name":"Madawaska River",
          "location":"Madawaska Blvd, Arnprior",
          "first_year":1966,
          "last_year":2010,
          "total_years":45,
          "missed_years":0,
          "long_coord":-76.34968485,
          "lat_coord":-76.34968485
        },
        "parm_desc":"ZINC,  UNFILTERED TOTAL"
      }
    ]
  }
]

There were some misplaced commas. http://jsonlint.com/ is a good tool.

Live demo of correct code and JSON (click).

m59
  • 43,214
  • 14
  • 119
  • 136