including data from static JSON file in Javascript

From: Ken Irwin <kirwin_at_nyob>
Date: Thu, 7 Apr 2016 01:02:46 +0000
To: CODE4LIB_at_LISTSERV.ND.EDU
Hi folks,

I'm working on a javascript project that currently has a bunch of JSON data defined inside the script; I'd like to move the data to a file outside the JS file so it can be updated without touching the script, but I'm running up against a few challenges externalizing the data.

The static JSON file lives in the same directory with the script.

If I had my druthers, I'd to it PHP style, but I don't think JS works this way:
=====
External file:
[all this JSON]

Script:
var data = include ('data.json');
====
All the options I find for loading external files are all AJAX-y, whereas what I really want is something synchronous - the script doesn't go on until the data loads.
I've also had some lexical scope issues where I can get the data inside the getJSON() function, but then have trouble transporting the data out of that function into the rest of the script.

Does anyone know of a good way to accomplish this? I imagine there's some incantation that I can perform, but I'm struggling to find it.

Thanks,
Ken
Received on Wed Apr 06 2016 - 21:03:38 EDT