4

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.

3
  • Maybe base64 encode it Commented May 20, 2021 at 3:53
  • it may be, blob is a datatype and you are returning string datatype Commented May 20, 2021 at 3:53
  • 2
    The blob isn't gone, it just is an object that has no enumerable properties: {}. What result do you expect? JSON has no type for binary data. Commented May 20, 2021 at 4:06

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

If it's to send that data to a server they'd be better sending it as multipart from the beginning. This encoding to JSON will be even bigger than a base64 encoding which is already ~37% bigger than the binary data it represents.
What do you think is a btter way ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.