Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,987 questions

51,931 answers

573 users

How to convert JSON to ArrayBuffer in JavaScript

2 Answers

0 votes
const jsonData = { name: "R2D2", age: 86, city: "New York" };

// Convert JSON to string
const jsonString = JSON.stringify(jsonData);

// Encode string to ArrayBuffer
const encoder = new TextEncoder();
const arrayBuffer = encoder.encode(jsonString).buffer;

console.log(arrayBuffer);



/*
run:

ArrayBuffer {
  [Uint8Contents]: <7b 22 6e 61 6d 65 22 3a 22 52 32 44 32 22 2c 22 61 67 65 22 3a 38 36 2c 22 63 69 74 79 22 3a 22 4e 65 77 20 59 6f 72 6b 22 7d>,
  byteLength: 42
}

*/

 



answered Dec 16, 2025 by avibootz
0 votes
// Step 1: JSON object
const jsonData = { name: "R2D2", age: 86, city: "New York" };

// Step 2: Convert to JSON string
const jsonString = JSON.stringify(jsonData);

// Step 3: Encode string into UTF-8 bytes
const encoder = new TextEncoder();
const uint8Array = encoder.encode(jsonString);

// Step 4: Get the underlying ArrayBuffer
const arrayBuffer = uint8Array.buffer;

console.log(arrayBuffer);



/*
run:

ArrayBuffer {
  [Uint8Contents]: <7b 22 6e 61 6d 65 22 3a 22 52 32 44 32 22 2c 22 61 67 65 22 3a 38 36 2c 22 63 69 74 79 22 3a 22 4e 65 77 20 59 6f 72 6b 22 7d>,
  byteLength: 42
}

*/

 



answered Dec 16, 2025 by avibootz
...