Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

323
Views
Javascript/Html: How to import local json / understanding module access

I am new to javascript / html and have some problems to understand the import / module functionality. I need to load a json file into javascript.

The best way I could find is via import, like:

  import myJason from './dummy.json' assert {type: 'json'};

When trying it, I am getting following error:

  Cannot use import statement outside a module

So I placed it into a module (tried header and body). But I am not able to access the module functions.

Google could not help me, could please somebody explain how to do it properly.

Note: HTML and JSON files are local files (not accessed via Webserver)

Here is the (reduced) code:

    <html lang="en">
        <head>
            <script type="module">
                import myJason from './dummy.json' assert {type: 'json'};
                function dummy(){
                    //evaluate JSON
                    return result();
                }
                window.dummy = dummy;
                export{dummy}

            </script>
        </head>
        <body>


            <script>
                // tried (and other things)
                //   dummy();
                //  window.dummy();
            </script>
        </body>
    </html>





  
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try using XML HttpRequest

var xhttp = new XMLHttpRequest();

function dummy(){
    if (this.readyState == 4 && this.status == 200) {
       return JSON.parse(xhttp.responseText)
    }
}

xhttp.onreadystatechange = dummy
xhttp.open("GET", "/dummy.json", true);
xhttp.send();

or even better the fetch api check that here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!