Decode a CBOR payload
Goal: Inspect a CBOR payload, either a binary file or a base64 string copied from a log or a token, as JSON. The reverse direction, encoding JSON as CBOR, is covered too.
Prerequisites: The built-in cbor module. Read a binary file with -I cbor. For a base64 string, read it with -I raw and call cbor_parse, which accepts base64 text as well as raw bytes.
Query
A binary file:
$ mq -I cbor -F json '.' payload.cbor
A base64 string:
$ echo 'o2JpZAdkdGFnc4JhYWFiYm9r9Q==' | mq -I raw -F json 'import "cbor" | cbor::cbor_parse'
Input
payload.cbor holds these 19 bytes (shown as hex), and the base64 string above is the same data:
a3 62 69 64 07 64 74 61 67 73 82 61 61 61 62 62 6f 6b f5
Output
Both commands print:
{
"id": 7,
"tags": [
"a",
"b"
],
"ok": true
}
Pick out one field
$ mq -I cbor 'get("tags")' payload.cbor
["a", "b"]
Encode JSON as base64 CBOR
$ echo '{"id": 7, "tags": ["a", "b"], "ok": true}' | mq -I json 'import "cbor" | cbor::cbor_stringify | base64'
o2JpZPlHAGR0YWdzgmFhYWJib2v1
Notes
cbor_stringifyreturns bytes. Printed directly they appear as hex (a3626964f94700…), and-owrites that hex text rather than a binary file. Pipe throughbase64when you need something you can paste elsewhere.- The base64 above differs from the decode example even though both describe the same data.
cbor_stringifywrites numbers as CBOR floats, so7becomesf94700instead of the one-byte integer07. Decoding either form gives7. - A truncated or invalid payload stops with a
Failed to parse CBORerror instead of returning partial data. - Once decoded, the data is an ordinary mq value, so
-Fcan write it in any output format, for example-F yamlor-F toml.