How to decode GZipped Base64 NBT to readable JSON in NodeJS

You can do this in Node.js using the zlib and buffer modules to decompress the Gzip data and then decode the Base64 encoded NBT (Named Binary Tag) data. Once you have the decoded NBT data, you can convert it to JSON using libraries like nbt-parser.

heres the step by step

  1. Install the required packages:

bashCopy code
npm install zlib buffer nbt-parser

  1. Write the Node.js code:

javascriptCopy code
const zlib = require(‘zlib’);
const Buffer = require(‘buffer’).Buffer;
const NbtParser = require(‘nbt-parser’);

// Function to decode GZipped Base64 NBT to JSON
async function decodeGzipBase64Nbt(gzippedBase64Nbt) {
try {
// Decode Base64
const base64Data = Buffer.from(gzippedBase64Nbt, ‘base64’);

// Decompress Gzip
const decompressedData = zlib.inflateSync(base64Data);

// Parse NBT data
const parsedData = NbtParser.parse(decompressedData);

// Convert to JSON
const jsonData = parsedData.toJson();

return jsonData;
} catch (error) {
console.error(‘Error decoding GZipped Base64 NBT:’, error);
throw error;
}
}

// Example usage
const gzippedBase64Nbt=”H4sIAAAAAAAAA+3Xy27DMBCF4f8K/G9p+xbXlrp2Dk4l0NmNQsbCqrJFItz…”; // Your GZipped Base64 NBT data here

decodeGzipBase64Nbt(gzippedBase64Nbt)
.then(jsonData => {
console.log(‘Decoded JSON:’, jsonData);
})
.catch(error => {
console.error(‘An error occurred:’, error);
});

Replace ‘Your GZipped Base64 NBT data here’ with your actual GZipped Base64 NBT data.

Read more here: Source link