Map
CBOR major type 5 encodes maps (key-value pairs). The argument gives the pair count. Keys and values can be any CBOR type – not just strings. 0xa2 0x61 0x61 0x01 0x61 0x62 0x02 = {"a":1, "b":2}. Integer keys are common in CBOR to reduce message size.
Encoding
CBOR maps differ from JSON objects in one critical way: keys can be any CBOR type, not just strings. Integer keys are common in CBOR-based protocols to minimize message size: {0: value, 1: value} is far smaller than {"algorithm": value, "key_ops": value}.
COSE (RFC 9052) and CWT (RFC 8392) use integer keys extensively. For example, COSE header parameter 1 = alg (algorithm), 3 = content_type, 4 = kid (key ID).
Map key uniqueness: RFC 8949 RECOMMENDS unique keys (avoid duplicates) but does not require it. Definite-length maps declare the pair count. Indefinite-length maps (0xbf) terminate with break 0xff.
Examples
| Label | Hex | Value |
|---|---|---|
| Empty | a0 | {} |
| {"a":1} | a1 61 61 01 | {"a": 1} |
| Integer keys | a2 00 01 01 02 | {0: 1, 1: 2} (integer keys) |
| {"a":1,"b":2} | a2 61 61 01 61 62 02 | {"a":1,"b":2} |