-1

I have a HTML file and at the bottom of the page is some JSON code.

<script id="__NEXT_DATA__" type="application/json">
{
    "props": {
        "pageProps": {
            "products": [

                {
                    "id": "1",
                    "name": "Product 1",
                    "description": "Some short description.",
                },
                {
                    "id": "2",
                    "name": "Product 2",
                    "description": "Some short description.",
                },

I am trying to move this JSON code into a sepparate file (data.json).

1 Answers1

-1

One approach would be to keep your HTML in one file and your data in another (i.e. props.json). If they were in the same folder, you could read the JSON data from the HTML file using the JQuery getJSON function, like this:

   <head> 
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    </head>
    
        <script>
        
            $(function() {
        
        
           var props = [];
        
           $.getJSON('props.json', function(data) {
               $.each(data.props, function(i, f) {
                  <DO STUFF HERE>
             });
        
           });
        
        });
       </script>
DanielG
  • 1,669
  • 1
  • 12
  • 26
  • It says: Uncaught ReferenceError: $ is not defined at index.html:292:13 – Jozef Pintér Apr 22 '22 at 14:45
  • Can you try adding this to your HTML file: – DanielG Apr 22 '22 at 14:46
  • @DanielG its a bad idea to suggest adding a library – Daniel A. White Apr 22 '22 at 14:48
  • Adding JQuery is a bad idea? – DanielG Apr 22 '22 at 14:49
  • @DanielG yes. its adding complexity and debt to a project that you don't control – Daniel A. White Apr 22 '22 at 15:06
  • Not to sound argumentative, but you sent him a reference post that uses JQuery and is effectively exactly what I suggested to him. How else would your suggestion have worked if he did not understand that your recommendation also requires JQuery? I understand your point, however, that if he naively adds JQuery without understanding what it is, as a dependency, he is encumbering complexity and debt. – DanielG Apr 22 '22 at 16:09