Skip to content

Instantly share code, notes, and snippets.

@nuclearglow
Created January 26, 2018 10:30
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save nuclearglow/ab251744db0ebddd504eea28153eb279 to your computer and use it in GitHub Desktop.
Save nuclearglow/ab251744db0ebddd504eea28153eb279 to your computer and use it in GitHub Desktop.
ArrayBuffer <-> JSON <-> ArrayBuffer
// array buffer to JSON
const dataString JSON.stringify(Array.from(new Uint8Array(arrayBuffer)));
// send around
// JSON to ArrayBuffer
new Uint8Array(JSON.parse(dataString)).buffer
@ErickWendel
Copy link

Hey there! I think your dataString is not working properlly

I tried like below and it worked

console.log(JSON.parse(String.fromCharCode.apply(null, new Uint8Array(data))));

@panelssh
Copy link

panelssh commented Jan 2, 2021

Hey there! I think your dataString is not working properlly

I tried like below and it worked

console.log(JSON.parse(String.fromCharCode.apply(null, new Uint8Array(data))));

Thanks, working good.

@mvdschee
Copy link

Thanks very useful :)

@jacksonkeating
Copy link

So many blog posts about this, and this answer is the best. 👍

@vaebe
Copy link

vaebe commented Dec 9, 2022

const enc = new TextDecoder("utf-8");
const data =new Uint8Array(res)
console.log(JSON.parse(enc.decode(data)))

@WillTechX20
Copy link

This was extremely helpful to me! This is truly an outstanding answer! Thank you, nuclearglow - you're a lifesaver!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment