JsonToCbor()
Converts a JSON string directly to CBOR (Concise Binary Object Representation) binary data. This is a shortcut when you already have JSON and want CBOR — it avoids the overhead of deserialising to CFML types first.
Requires Extension: Crypto Extension
JsonToCbor( json=string );
Returns: binary
| Argument | Description |
|---|---|
|
json
string,
required
|
edit
JSON string to convert |
Usage Notes
editThis converts directly from JSON to CBOR using the underlying library — it does not go through CFML types, avoiding the intermediate conversion step of CborEncode( deserializeJSON( json ) ).
Key types: JSON keys are always strings, so JsonToCbor produces CBOR text-string keys. If you need CBOR integer keys (e.g. for COSE), use CborEncode() with a CFML struct instead — it converts numeric-looking string keys to CBOR integers automatically.
Examples
edit// Convert JSON directly to CBOR binary data
json = '{"name":"test","value":42}';
cbor = JsonToCbor( json );
// cbor is binary CBOR data
// Verify the roundtrip
backToJson = CborToJson( cbor );
parsed = deserializeJSON( backToJson );
// parsed.name == "test", parsed.value == 42
// Convert a JSON array
cbor = JsonToCbor( '[1,2,3,"four"]' );
// Encode existing API response data as CBOR for a system that expects it
apiResponse = '{"status":"ok","data":[1,2,3]}';
cborPayload = JsonToCbor( apiResponse );
// Send cborPayload to a CBOR-expecting endpoint
See also
- Cryptography
- CborEncode()
- CborToJson()
- Search Issue Tracker open_in_new
- Search Lucee Test Cases open_in_new (good for further, detailed examples)