Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.stringify an object with a blob

I have an object with an audio blob inside of it. When I call JSON.stringify on the object, the blob disappears. How to stringify a binary blob in an object ?

The audio blob is from the sox-element and in this case is of mime type 'audio/wav'.

let blob = this/soxElem.getBlob();
let object = {
  audio: blob,
  name: "hi"
}

console.log(JSON.stringify(object))

The console shows {audio:{}, name: "hi"}. The blob is gone.

like image 613
Matt Avatar asked Oct 16 '25 02:10

Matt


1 Answers

One way to do it is to convert to an array which JSON can handle :

let ab = await this.soxElem.getBlob().arrayBuffer();
let object = {
  audio: Array.from(new Uint8Array(ab)),
  name: "hi"
}

On the receiving side (e.g. with Node.js) decode it like so :

let binaryData = Buffer.from(data.audio);
like image 59
Matt Avatar answered Oct 18 '25 16:10

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!