CBOR to JSON Converter

CBOR to JSON

About CBOR

What is CBOR?

CBOR (RFC 8949) is an IETF binary data format. This converter uses standard maps rather than library-specific record extensions and round-trips byte strings, large integers, floats, arrays, maps with non-string keys, dates, undefined values, and semantic tags through JSON wrappers.

When to use CBOR?

Use CBOR when peers need typed values unavailable in JSON, but agree on semantic-tag handling and any deterministic encoding profile. This converter does not promise canonical bytes; preserve its $binary, $map, $cborTag, $date, $undefined, and integer wrappers when editing JSON.

CBOR in JavaScript

import { encode, decode } from 'cbor-x';

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: a16568656c6c6f65776f726c64

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

CBOR in Go

package main

import (
	"fmt"

	"github.com/fxamacker/cbor/v2"
)

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

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

CBOR in PHP

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

use CBOR\Decoder;
use CBOR\MapObject;
use CBOR\StringStream;
use CBOR\TextStringObject;

$value = MapObject::create()
	->add(TextStringObject::create('hello'), TextStringObject::create('world'));

$encoded = (string) $value;
echo bin2hex($encoded) . PHP_EOL;
// Output: a16568656c6c6f65776f726c64

$decoded = Decoder::create()->decode(StringStream::create($encoded))->normalize();
echo $decoded['hello'] . PHP_EOL;
// Output: world