MessagePack to JSON

About MessagePack

What is MessagePack?

MessagePack is a compact binary serialization format for JSON-shaped data, including strings, numbers, arrays, key-value maps, binary values, timestamps, and extension values.

When to use MessagePack?

Use MessagePack for compact API payloads, cache entries, queue messages, and logs when both sides understand MessagePack and you want smaller binary data while keeping a JSON-shaped data model.

MessagePack in JavaScript

import { encode, decode } from '@msgpack/msgpack';

const value = { hello: 'world' };
const bytes = encode(value);
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');

console.log(hex);
// Output: 81a568656c6c6fa5776f726c64

console.log(decode(bytes));
// Output: { hello: 'world' }
				

MessagePack in Go

package main

import (
	"fmt"

	"github.com/vmihailenco/msgpack/v5"
)

func main() {
	value := map[string]any{"hello": "world"}
	encoded, _ := msgpack.Marshal(value)
	fmt.Printf("%x\n", encoded)
	// Output: 81a568656c6c6fa5776f726c64

	var decoded map[string]any
	_ = msgpack.Unmarshal(encoded, &decoded)
	fmt.Println(decoded["hello"])
	// Output: world
}
				

MessagePack in PHP

<?php
require 'vendor/autoload.php';

use MessagePack\MessagePack;

$value = ['hello' => 'world'];
$encoded = MessagePack::pack($value);
echo bin2hex($encoded) . PHP_EOL;
// Output: 81a568656c6c6fa5776f726c64

$decoded = MessagePack::unpack($encoded);
echo $decoded['hello'] . PHP_EOL;
// Output: world