UBJSON to JSON Converter

UBJSON to JSON

About UBJSON

What is UBJSON?

UBJSON is a marker-based binary encoding for JSON values. This converter decodes null and booleans, integer and floating types, high-precision numbers, characters and strings, arrays, objects, no-op markers, typed or counted containers, and optimized uint8 arrays; encoding also accepts JSON wrappers for binary, int64, float64, and high-precision values.

When to use UBJSON?

Use UBJSON mainly for existing integrations. Confirm that the peer supports $ typed and # counted containers plus the H high-precision marker; this converter emits counted containers and optimized uint8 arrays, and it rejects unsupported markers or trailing bytes.

UBJSON in JavaScript

const UBJSON = require('ubjson');

// The npm package is legacy; use it mainly for compatibility with existing UBJSON data.
const value = { hello: 'world' };
const buffer = Buffer.alloc(1024);
const offset = UBJSON.packToBufferSync(value, buffer);
const encoded = buffer.subarray(0, offset);

console.log(encoded.toString('hex'));
UBJSON.unpackBuffer(encoded, (error, decoded) => {
	if (error) throw error;
	console.log(decoded);
});
			

UBJSON in Go

package main

import (
	"fmt"

	"github.com/jmank88/ubjson"
)

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

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

UBJSON in PHP

<?php
// PHP does not have a widely used maintained UBJSON package.
// This writes the common optimized uint8 array form used for binary payloads.
$bytes = "\x01\x02\x03";
$encoded = '[' . '$' . 'U' . '#' . 'U' . chr(strlen($bytes)) . $bytes;

echo bin2hex($encoded) . PHP_EOL;
// Output: 5b2455235503010203