// Modified function
interface BufferObject {
  type: string;
  data: number[];
}

function ConvertBytesToJson(jsonData: string, callback?: (key: string, value: string) => void): { key: string; value: string } {
  // Default callback to store key-value pair
  const defaultCallback: (key: string, value: string) => void = (key, value) => {
    keyValueObject.key = key;
    keyValueObject.value = value;
    console.log(`Key: ${key}`);
  };

  // Use the provided callback or the default one
  const actualCallback = callback || defaultCallback;

  // Object to store key-value pair
  const keyValueObject: { key: string; value: string } = { key: '', value: '' };

  // Parse the JSON string into a TypeScript object
  const bufferObject: BufferObject = JSON.parse(jsonData);

  // Convert the numeric values in the "data" array to a Buffer
  const buffer = Buffer.from(bufferObject.data);

  // Decode the Buffer to a string (assuming it contains UTF-8 encoded text)
  const decodedString = buffer.toString('utf-8');

  // Invoke the callback for each key-value pair
  const jsonObject: { [key: string]: string } = JSON.parse(decodedString);
  for (const key in jsonObject) {
    if (Object.prototype.hasOwnProperty.call(jsonObject, key)) {
      const value = jsonObject[key];
      actualCallback(key, value);
    }
  }

  // Return the key-value object
  return keyValueObject;
}
// Example usage
const jsonData =
  '{"type":"Buffer","data":[123,34,110,97,109,101,34,58,34,68,114,46,32,70,114,97,110,99,105,115,32,84,101,114,114,121,34,125]}';
// Decode the buffer and retrieve the key-value object
console.log(ConvertBytesToJson(jsonData).key);

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: