Let's say I'm doing an AJAX call to get some JSON data and it returns a 300MB+ JSON string. After it finishes downloading, I need to parse it.
JSON.parse(this.responseText);
In Chrome (v56) I'll get this error: “Uncaught SyntaxError: Unexpected end of JSON input”
If I download that same JSON string and open it up in Notepad.exe, after a minute or two of waiting, I can navigate to the bottom and it appears as if the entire JSON string looks good (it ends with the proper closing tags).
Is there a limitation to JSON.parse()
? It would surprise me if Chrome is simply unable to convert the JSON string to a proper JSON object because it's such a large size. While this is sort of being used in a stress-test scenario, I would like to see this working with JSON strings as large as 500MB. Note: Using JSON.parse(this.responseText)
, I have no issues with 200MB JSON strings.
(I understand that there is supposedly no limitation on JSON size. I am not asking that question. I am asking if there is an issue with JSON.parse at a certain file size or certain memory amount or if there is a different line of code that needs to be run)
Anyone know of a solution (client-side is ideal as opposed to paginating the JSON or breaking it up server side and making multiple calls)? Thoughts?
UPDATE:
If I grab a smaller JSON string and do not parse it after the AJAX response, I can do a console.log(this.responseText.length);
It returns 11514
in the DevTools.
For the the ~300MB JSON string, console.log(this.responseText.length);
returns 0
.
AJAX call:
getData: function(url){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(this.readyState === 4 && this.status === 200){
console.log(this.responseText.length);
return false; //prevent from going to next line to parse
var data = JSON.parse(this.responseText);
}
};
xhr.onerror = function(){
console.log('there was an error:', this);
};
xhr.open('GET', url, true);
xhr.send();
}
Thoughts? I can't be the only one who's run into this issue.