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

241
Views
Workaround for GET XMLHttpRequest (local request) size limits

Problem

Whilst developing a chrome extension I encountered a problem where I try to retrieve large JSON file (~1GB) using XMLHttpRequest GET request. The file is located on my local machine and I retrieve the download link using chrome.runtime.getURL('data.json') from a content script.

The error I am getting is

Uncaught SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at xhr.onreadystatechange (<anonymous>:11:23)
    at XMLHttpRequest.<anonymous>

I assumed it is because the JSON file is too large and therefore, it is not possible to download it in one go (please correct me if this wouldn't be the case). Is there any workaround for this size limitation?

Additional info

I generate the JSON file from a python script, thus, it is completely possible to choose a different format (some binary format maybe) if necessary.

Furthermore, I cannot use any API provided for chrome extensions since the script I am actually calling the XMLHttpRequest from is not a content script but an appended script to the body of the website.

Here is a minimal code for reproduction (don't forget to configure the content_security_policy in the manifest to enable inline script execution).

// Content script

function init() {

    let script = document.createElement("script");
    script.appendChild(document.createTextNode(__magic.toString() + "; __magic('" + chrome.runtime.getURL('data.json') + "');"));
    script.id = "__magic";
    document.body.appendChild(script);
}

function __magic(url) {
    var db;
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = () => { 
        if (xhr.readyState === 4) {
            db = JSON.parse(xhr.response);
            window.setInterval(check, 10);
        }
    };
    xhr.open("GET", url, true);
    xhr.send();
}

init();

Edit


I forgot to say that I was successful in loading the big JSON file in python, thus, I think it is safe to assume that the JSON file is not corrupted.

Furthermore, the JSON file is storing a graph structure - I choose following layout for the json file

{
 "node1": {
   "children": ["node2", "node3"],
   "foo": 15
 },
 "node2": {
   "children": ["node15", "node1"],
   "foo": 90
 },
 ...
}

I bet there is a more efficient way of storing a graph structure rather than using JSON, however, so far I could not find any that would be compatible with javascript.

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

0

  1. save the response in a new file and confront the size with the original file.
  2. try with xhr.responseType = "json" and no parse the response (if you can)
  3. try to split the local file into smaller chunks and make several asyncromous xmlhttprequest\fetch requests (Promise.all) and then joint the responses into your big final json file.
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!