0

I am trying to embed the content of a JSON file data.json into a Single Page Application (SPA). One way to do this is to embed the content directly as data using a script tag, per this link: Embeding Arbitrary JSON.

However, this approach is not perfect in my case, because:

  1. data.json is 1000+ lines of code;
  2. data.json is used in several places, including the page in discussion; and I want to keep just one copy of data.json;

I wonder if it's possible to have a pointer in the page to point to the external JSON file?

Note that this question is different from this question.

lgc_ustc
  • 1,524
  • 2
  • 20
  • 31

1 Answers1

2

For php you can just use something like this

<script>
var data = <?php echo file_get_contents('PATH_TO_JSON_FILE'); ?>;
</script>
Manuel Otto
  • 6,410
  • 1
  • 18
  • 25