Loading CBOR Formatter...
Please wait a moment

How to Format CBOR

Encoders, test vectors and RFC examples tend to hand you CBOR either as one long line of diagnostic notation or as raw bytes in Hex. Paste either one below to get it laid out one item per line.

Step 1

Paste diagnostic notation or CBOR bytes

Paste minified diagnostic notation, or the CBOR bytes as Hex or Base64. You can also upload a .cbor file or a text file holding any of these. The Sample button loads a one-line example with a byte string, two tags and an indefinite-length map.

•
Diagnostic notation: copy it from a debugger, an RFC or a test file. Plain JSON works too, since it is a subset.
•
Hex or Base64: spaces, commas, colons and 0x prefixes in Hex are fine, and Base64 may be URL-safe or wrapped over several lines.
•
A file: click Upload and pick a binary .cbor file, or a .txt file holding diagnostic notation, Hex or Base64.
Step 2

Read the indented result

About half a second after you stop typing, the output shows the data with 2-space indentation, one item per line. If something is wrong, you get the line and column of the problem instead.

•
Values untouched: text you paste is only re-spaced, so numbers like 14.50 and strings like "00501" keep their exact spelling. Comments are left out.
•
Bytes decoded as stored: integers keep every digit, byte strings show as h'…', and tags, undefined and indefinite lengths appear as they are in the data.
•
Errors with a location: a missing comma or bracket in the text, or a bad character or cut-off value in the bytes, is reported with where it happened.
Step 3

Copy or download it

Copy puts the formatted text on your clipboard; Download saves it as formatted-cbor.txt.

•
Into your editor or docs: paste it into your code editor, a README or a spec.
•
Need JSON instead? CBOR to JSON turns the same bytes into JSON, and the CBOR Validator checks a file.

Frequently Asked Questions

What is CBOR diagnostic notation?

It is the text form of CBOR described in section 8 of RFC 8949. It looks like JSON but can also write the things JSON has no syntax for: byte strings such as h'00ff', tags such as 1(1363896240), undefined, simple(16), indefinite-length arrays written [_ 1, 2], and map keys that are numbers. RFCs, COSE and CWT examples and most CBOR debugging tools print data this way.

What can I paste: text or bytes?

Either. Diagnostic notation (or plain JSON) is re-indented as text. CBOR bytes written as Hex (a1 61 61 01, 0xa1, 0x61, … or one long run of digits) or as Base64 (plain or URL-safe, with or without padding) are decoded and printed as diagnostic notation. A binary .cbor file you upload is shown as Base64 and decoded the same way. The badge above the output says which reading was used.

Does formatting change any values?

No. Text you paste is only re-spaced: every number, string and h'…' literal keeps the exact spelling you typed, so 14.50 stays 14.50 and 8901240544102066246 keeps all its digits. The one thing that is dropped is comments (/ … / and # …), and a note says so when that happens.

How are numbers shown when I paste Hex or Base64?

Integers are printed with every digit, including 64-bit values like -9223372036854775808 and 18446744073709551615. Floats always show a decimal point or an exponent (2.0, not 2), so you can tell them from integers, and NaN, Infinity and -0.0 are written out. A bignum stays as its tag, for example 2(h'400000000000000000'), exactly as it is stored.

How is this different from CBOR to JSON?

CBOR to JSON gives you JSON you can load in any program, so CBOR-only types have to be translated: byte strings become Base64 text and tags become small objects. This formatter keeps the CBOR view instead, with h'…' bytes, tags, undefined and number keys left as they are, which is what you want when you are debugging an encoder or writing documentation.

What does an error like "Line 1, column 12" mean?

For diagnostic notation it points at the spot where the text stops making sense, such as a missing comma, an unclosed bracket or a word without quotes. For Hex and Base64 it names the character that does not belong, or says the data looks cut off when the bytes end in the middle of a value.

Is my data sent anywhere?

No. Formatting and decoding run in JavaScript in your browser and the download is built on your own machine. Nothing is uploaded and nothing is kept, so a reload starts with an empty editor.

The notation is defined in RFC 8949, section 8. For plain JSON, the JSON Formatter has more options.