You're sending JSON:

return res.status(200).json(imgProcessed)

That means you're sending text, not binary data.

You probably wanted to send just the data:

return res.status(200).send(imgProcessed)
// --------------------^^^^

I'm assuming imgProcessed is a Buffer; if not you want to make it one. See the Express.js documentation for send. (It will handle sending the Content-Type: application/octet-stream header for you when you give it a Buffer.)

Answer from T.J. Crowder on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8ClampedArray › Uint8ClampedArray
Uint8ClampedArray() constructor - JavaScript - MDN Web Docs
July 10, 2025 - The contents are initialized to 0 unless initialization data is explicitly provided. js · new Uint8ClampedArray() new Uint8ClampedArray(length) new Uint8ClampedArray(typedArray) new Uint8ClampedArray(object) new Uint8ClampedArray(buffer) new Uint8ClampedArray(buffer, byteOffset) new Uint8ClampedArray(buffer, byteOffset, length) Note: Uint8ClampedArray() can only be constructed with new.
🌐
JavaScripture
javascripture.com › Uint8ClampedArray
Uint8ClampedArray JavaScript API
Gets and sets the element in this at index. index should be between 0 and this.length - 1. Setting an element to a value less than 0 will store 0 and greater than 255 will store 255 in the array. ... Returns the underlying buffer for this Uint8ClampedArray.
🌐
GitHub
gist.github.com › jonathanlurie › 04fa6343e64f750d03072ac92584b5df
Serialize/deserialize json and conserve typed arrays · GitHub
The only problem I found was the lack of support for ArrayBuffer values. To support it, you may use the data: Array.apply([], value instanceof ArrayBuffer ? new Uint8Array(value) : value), for serialization and for unserialization check and return if (value.constructor === 'ArrayBuffer') return new Uint8Array(value.data).buffer;.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8ClampedArray
Uint8ClampedArray - JavaScript - MDN Web Docs
July 10, 2025 - The Uint8ClampedArray typed array represents an array of 8-bit unsigned integers clamped to 0–255. The contents are initialized to 0 unless initialization data is explicitly provided. Once established, you can reference elements in the array using the object's methods, or using standard array index syntax (that is, using bracket notation).
🌐
Stack Overflow
stackoverflow.com › questions › 39087314 › pipe-buffer-image-data-into-uint8clampedarray-format-in-node-js
Pipe buffer image data into Uint8ClampedArray format in Node.js
August 24, 2018 - I've tried a ton of things and the only process that actually shows me an array is buffer.toJSON().data. Looking at that, the numbers are all <= 255, however I can't see any every fourth number is 255 pattern which indicates something is wrong. ... How can I get an Uint8ClampedArray with the proper [r1, g1, b1, a1, r2, g2, b2, a2.....] formatting from the image buffer in Node.js that I can then pass to my function?
🌐
GitHub
github.com › lovell › sharp › issues › 549
Pipe buffer image data into Uint8ClampedArray format · Issue #549 · lovell/sharp
August 22, 2016 - I've tried a ton of things and the only process that actually shows me an array is buffer.toJSON().data. Looking at that the numbers are all at or below 255, however I can't see any every fourth number is 255 pattern which indicates something is wrong. ... How can I get an Uint8ClampedArray with the proper [r1, g1, b1, a1, r2, g2, b2, a2.....] formatting from the image buffer in node that I can then pass to my function?
Author: lovell
🌐
Bun
bun.com › binary data › convert a uint8array to a buffer
Convert a Uint8Array to a Buffer - Bun
July 25, 2026 - const arr: Uint8Array = ... const buf = Buffer.from(arr);
Top answer
1 of 2
14

Please visit this answer https://stackoverflow.com/a/12776856/16315663 to retrieve GZIP data from the response.

Assuming, You have already retrieved full data as UInt8Array.

You just need the UInt8Array as String

const jsonString = Buffer.from(dataAsU8Array).toString('utf8')

const parsedData = JSON.parse(jsonString)

console.log(parsedData)

Edit

Here is what worked for me

const {request} = require("https")
const zlib = require("zlib")


const parseGzip = (gzipBuffer) => new Promise((resolve, reject) =>{
    zlib.gunzip(gzipBuffer, (err, buffer) => {
        if (err) {
            reject(err)
            return
        }
        resolve(buffer)
    })
})

const fetchJson = (url) => new Promise((resolve, reject) => {
    const r = request(url)
    r.on("response", (response) => {
        if (response.statusCode !== 200) {
            reject(new Error(`${response.statusCode} ${response.statusMessage}`))
            return
        }

        const responseBufferChunks = []

        response.on("data", (data) => {
            console.log(data.length);
            responseBufferChunks.push(data)
        })
        response.on("end", async () => {
            const responseBuffer = Buffer.concat(responseBufferChunks)
            const unzippedBuffer = await parseGzip(responseBuffer)
            resolve(JSON.parse(unzippedBuffer.toString()))
        })
    })
    r.end()
})

fetchJson("https://wiki.mozilla.org/images/f/ff/Example.json.gz")
    .then((result) => {
        console.log(result)
    })
    .catch((e) => {
        console.log(e)
    })
2 of 2
0

Thank you, I actually just tried this approach and I get the following error:

SyntaxError: JSON Parse error: Unexpected identifier "x"

But I managed to print the data in text format using the below function:

getFinancialReports = (options, callback) => {
    // buffer to store the streamed decompression
    var buffer = [];

    https
      .get(options, function (res) {
        // pipe the response into the gunzip to decompress
        var gunzip = zlib.createGunzip();
        res.pipe(gunzip);

        gunzip
          .on("data", function (data) {
            // decompression chunk ready, add it to the buffer
            buffer.push(data.toString());
          })
          .on("end", function () {
            // response and decompression complete, join the buffer and return
            callback(null, buffer.join(""));
          })
          .on("error", function (e) {
            callback(e);
          });
      })
      .on("error", function (e) {
        callback(e);
      });
  };

Now I would need to pass this into a JSON object.

Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-uint8clampedarray-from-method
JavaScript Uint8ClampedArray.from() Method | GeeksforGeeks
July 11, 2023 - The Uint8ClampedArray represents an array of 8-bit unsigned integers clamped to 0-255. If a value is specified that is not in the range of [0, 255], 0, or 255 will be set instead; if the specified value is not an integer, the nearest integer ...
🌐
W3cubDocs
docs.w3cub.com › javascript › global_objects › uint8clampedarray
Uint8ClampedArray - JavaScript - W3cubDocs
August 7, 2022 - The Uint8ClampedArray typed array represents an array of 8-bit unsigned integers clamped to 0–255. The contents are initialized to 0. Once …
🌐
MDN
mdn2.netlify.app › en-us › docs › web › javascript › reference › global_objects › uint8clampedarray › uint8clampedarray
Uint8ClampedArray() constructor - JavaScript | MDN
February 18, 2022 - // From a length var uintc8 = new ... console.log(y[0]); // 21 // From an ArrayBuffer var buffer = new ArrayBuffer(8); var z = new Uint8ClampedArray(buffer, 1, 4); // From an iterable var iterable = function*(){ yield* [1,2,3]; ...
🌐
MDN
mdn2.netlify.app › en-us › docs › web › javascript › reference › global_objects › uint8clampedarray
Uint8ClampedArray - JavaScript | MDN
February 18, 2022 - Returns the length (in bytes) of the Uint8ClampedArray from the start of its ArrayBuffer.
🌐
UDN Web Docs
udn.realityripple.com › docs › Mozilla › js-ctypes › Using_js-ctypes › Working_with_ArrayBuffers
Working with ArrayBuffers - UDN Web Docs: MDN Backup
// pixelBuffer is a pointer to ... imgHeight = 400; var myImgDat = new ImageData(imgWidth, imgHeight); One method is to get into a js-ctypes array, and then set it into the ImageData, as illustrated by the following code example....
🌐
Telerik
telerik.com › blogs › array-buffers-javascript
Array Buffers in JavaScript
October 22, 2025 - Uint8ClampedArray: 8-bit clamped unsigned integers (1 byte, range: 0 to 255, but values outside this range are “clamped” to the nearest valid value)
🌐
Salesforce
documentation.b2c.commercecloud.salesforce.com › DOC3 › topic › com.demandware.dochelp › DWAPI › scriptapi › html › api › class_TopLevel_Uint8ClampedArray.html
Class Uint8ClampedArray | Salesforce B2C Developer Documentation Resources
The start offset for this typed array within the array buffer. ... The number of elements. ... Creates an empty array. ... Creates an array with the given element count. ... Creates an array as a copy of the passed typed array. ... Creates an array as a copy of the passed array. Uint8ClampedArray(buffer : ArrayBuffer, byteOffset : Number, length : Number)