Situation:
Within my application, I am encountering the following code:
let blob = new Blob([JSON.stringify(json)], {type: "application/json"});
This code sometimes fails because the maximum string length allowed in Chrome is approximately 500MB, and the size of the json
object can exceed this limit.
Inquiry:
I am seeking a method to directly convert my json
variable (a POJO) into a Blob, potentially through a streaming process that converts it to an ArrayBuffer incrementally. Alternatively, I am open to any other approach that enables the conversion of a large json
object into a Blob without encountering the 'maximum string length' issue.
Considerations:
- The proposed solution must be functional in web browsers.
- If recommending an existing library, it should not require the
json
object to be just an array, as handling such cases is relatively straightforward. The library should instead support arbitrarily nested JSON objects where a significant portion of data may reside deep within the structure rather than being evenly distributed across top-level keys. - I am not interested in solutions that necessitate a stream as input and yield a stream of string segments as output, like json-stream-stringify or streaming-json-stringify. My preference is for transforming an already-loaded POJO into a Blob containing the stringified JSON content.
Additional Information:
- How to use JSONStream to stringify a large object - This reference shares similarities with my scenario but focuses on
JSONStream
, designed for Node.js rather than browser environments. Additionally, the provided solution appears to save data key-by-key rather than in a deeply nested manner. If there exists a way to achieve this functionality in a web browser, resulting in an ArrayBuffer storing the oversized JSON string for complex nested objects, that would be considered a suitable answer. - How to use streams to JSON stringify large nested objects in Node.js? - Similar to the previous link.