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.

4
  • 1
    How about some ajax? Commented Aug 28, 2017 at 0:18
  • Yeah, Ajax would do. But I still wonder if it's possible to embed it into our page.
    – lgc_ustc
    Commented Aug 28, 2017 at 0:20
  • How do you serve the html? PHP? Node.js? or just static html pages? Commented Aug 28, 2017 at 0:21
  • It's a Single Page Application (SPA) served by Apache/PHP.
    – lgc_ustc
    Commented Aug 28, 2017 at 0:22

1 Answer 1

2

For php you can just use something like this

<script>
var data = <?php echo file_get_contents('PATH_TO_JSON_FILE'); ?>;
</script>
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.