You can use TextDecoder:

try {
  parsedJson = JSON.parse(new TextDecoder().decode(response as ArrayBuffer));
} catch (e) {
  parsedJson = {};
}

In my case the request is waiting for an ArrayBuffer response, but if an error occours on the backend, I get a JSON object containing the error. I check if the response is an error with this code.

The try-catch block is needed because if the ArrayBuffer is not a json, then the JSON.parse() method will throw an error.

Answer from Bence Szabó on Stack Overflow
🌐
GitHub
gist.github.com › nuclearglow › ab251744db0ebddd504eea28153eb279
ArrayBuffer <-> JSON <-> ArrayBuffer · GitHub
To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ... Thanks, working good. ... So many blog posts about this, and this answer is the best. 👍 ... const enc = new TextDecoder("utf-8"); const data =new Uint8Array(res) console.log(JSON.parse(enc.decode(data)))
Discussions

How to convert arrayBuffer to JSON in React
I am using Axios and I am getting arraybuffer as a response. I want to convert arraybuffer to json. I really tried hard but didn't find a proper solution. Could someone please help me. Thanks axios... More on stackoverflow.com
🌐 stackoverflow.com
JS: How to convert ArrayBuffer to string?
Convert it to a Uint8Array, then convert that to a base64 string with btoa: https://stackoverflow.com/questions/9267899/arraybuffer-to-base64-encoded-string More on reddit.com
🌐 r/learnprogramming
1
1
May 2, 2023
How to read JSON error response from $http if responseType is arraybuffer
Basically you just need to decode the ArrayBuffer into a string and use JSON.parse(). More on stackoverflow.com
🌐 stackoverflow.com
node.js - Typescript how to use ArrayBuffer data made of JSON - Stack Overflow
I am receiving ArrayBuffer data from a server via Websocket, and having a trouble converting the incoming data into other form. Here's the code for the WebSocket: let ws = new WebSocket('wss://api. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
medium.com › @techsuneel99 › how-to-convert-a-buffer-to-json-in-node-js-4fa9ef98160
How to convert a Buffer to JSON in Node.js ? | by Suneel Kumar | Medium
December 31, 2023 - const buf = Buffer.from([0x1, 0x2, 0x3]); // Encode the Buffer to Base64 const base64Data = buf.toString('base64'); // Create a JSON object with the Base64-encoded data const json = { data: base64Data }; // Convert the JSON object to a string const jsonString = JSON.stringify(json); console.log(jsonString); //output {"data":"AQID"} By default, Buffer.toString() uses base64 encoding. You can explicitly specify utf8 encoding: // Function to encode a string to UTF-8 function utf8Encode(str) { const encoder = new TextEncoder('utf-8'); return encoder.encode(str); } // Function to decode a UTF-8 enc
🌐
Reddit
reddit.com › r/learnprogramming › js: how to convert arraybuffer to string?
r/learnprogramming on Reddit: JS: How to convert ArrayBuffer to string?
May 2, 2023 -

Working with Web Push Notifications.

https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription

I got the Push Subscription Object.

I get the keys for p256dh and auth, using PushSubscription.getKey()

Unfortunately, getKey() returns an ArrayBuffer per the docs.

I need to save this subscription object with JSON.stringify(), specifically because I am creating a new object and can't use the toJSON() method which is part of the original Push Subscription Object.

When converting to JSON , ArrayBuffer disappears. So I get an empty object.

Thus, I need to convert the ArrayBuffer to a string before sending to the server.

How do I do this? I saw the Buffer suggested, but thats only available on server side.

🌐
npm
npmjs.com › package › json-arraybuffer-reviver
json-arraybuffer-reviver - npm
The replacer function looks at the data that you are trying to encode into JSON and instead of using the default toString() methods it encodes the data in a human readable form as follows: ... { $ArrayBuffer: { View: "Name of the ArrayBuffer ...
      » npm install json-arraybuffer-reviver
    
Published: Aug 30, 2024
Version: 2.0.0
Author: Jonathan Silverblood
🌐
Medium
medium.com › hackernoon › https-medium-com-amanhimself-converting-a-buffer-to-json-and-utf8-strings-in-nodejs-2150b1e3de57
Converting a Buffer to JSON and Utf8 Strings in Nodejs | by Aman Mittal | HackerNoon.com | Medium
April 1, 2019 - The JSON specifies that the type of object being transformed is a Buffer, and its data. .toString() is not the only way to convert a buffer to a string.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › ArrayBuffer
ArrayBuffer - JavaScript - MDN Web Docs
It is an array of bytes, often referred to in other languages as a "byte array". You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.
🌐
Azurewebsites
benchmarklab.azurewebsites.net › Benchmarks › Show › 20014 › 0 › arraybuffer-vs-json-serialization------v3
Benchmark: ArrayBuffer vs JSON serialization ---- v3 - MeasureThat.net
✓ ArrayBuffer serialization: 4,949 ops/sec · • JSON serialization: 1,860 ops/sec · Results show median performance across all community submissions. Run this benchmark yourself to verify on your device. Rendered benchmark preparation results: Run tests (2) Previous results Fork ·
🌐
json-bufferify
langzhai.github.io › json-bufferify
json-bufferify
offset - The start point of the DataView where to store the data. ... Revert JSON data from ArrayBuffer and return it.
🌐
MeasureThat
measurethat.net › Benchmarks › Show › 1395 › 0 › arraybuffer-vs-json-serialization
Benchmark: ArrayBuffer vs JSON serialization - MeasureThat.net
Lodash cloneDeep vs structuredClone vs JSON.parse + JSON.stringify but with big data · Comments · × · Do you really want to delete benchmark? Cancel Delete · × · FAQ: FAQ · Source code: GitHub/MeasureThat.net · Report issues: MeasureThat.net/Issues ·
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-json-to-arraybuffer-in-javascript
How to Convert JSON to ArrayBuffer in JavaScript? - GeeksforGeeks
September 3, 2024 - An ArrayBuffer is a complex data type, and structures which has a fixed length and takes binary content as the whole. Any variable that contains pure binary data will be defined in JavaScript as Simple Data, however on some occasions it is sometimes necessary to convert JSON data to an ArrayBuffer, for example when the objective is to manipulate images or other forms of non-text data that involve using web application program interfaces that accept binary input.
🌐
GitLab
gitlab.com › jonathan silverblood › json-arraybuffer-reviver
Jonathan Silverblood / json-arraybuffer-reviver · GitLab
December 10, 2022 - Utility functions for JSON.parse and JSON.stringify to work with typed arrays and array buffers.
🌐
npm
npmjs.com › package › json-bufferify
json-bufferify - npm
March 11, 2025 - offset - The start point of the DataView where to store the data. ... Revert JSON data from ArrayBuffer and return it.
      » npm install json-bufferify
    
Published: Mar 11, 2025
Version: 0.2.3
Author: LangZhai
Starred by 28 users
Forked by 2 users
Languages: JavaScript