Overview
Introduction
Bencode is a niche but very stable format, it's what every .torrent file and BitTorrent tracker response is written in, and it hasn't meaningfully changed since the original BitTorrent spec.
This tool encodes a JSON value directly into Bencode, useful for building test fixtures, debugging a torrent parser, or learning how the format's four types map onto JSON's data model.
What Is JSON to Bencode Converter?
A Bencode encoder that converts a JSON value into Bencode's four primitives: byte strings (len:data), integers (iNe), lists (l...e), and dictionaries (d...e).
Object keys are sorted lexicographically before encoding, since the Bencode spec requires sorted dictionary keys for a canonical encoding.
How JSON to Bencode Converter Works
Strings are prefixed with their UTF-8 byte length and a colon (3:abc), numbers are wrapped in i...e, arrays become l...e with each item encoded in sequence, and objects become d...e with entries emitted in sorted key order.
Each value's encoding is fully self-delimiting, there's no fixed-width header to compute, so the encoder just concatenates each piece's text representation as it recurses through the JSON structure.
When To Use JSON to Bencode Converter
Use it when building a test fixture for a BitTorrent client, tracker, or .torrent file parser and you need known-correct Bencode bytes from a JSON description.
It's also useful for learning Bencode's encoding rules by comparing a JSON value against its encoded form side by side.
Often used alongside Bencode to JSON Converter, JSON to BSON Converter and JSON to UBJSON Converter.
Features
Advantages
- Produces spec-compliant Bencode with correctly sorted dictionary keys.
- Handles arbitrarily nested lists and dictionaries, not just a flat structure.
- Runs entirely client-side, so no torrent or tracker data leaves the browser.
Limitations
- Non-integer numbers are rounded, since Bencode's integer type has no fractional representation.
- null encodes as an empty string, a JSON-specific convention rather than a Bencode-defined mapping, since the format has no native null type.
Examples
Best Practices & Notes
Best Practices
- Stick to whole numbers in your JSON source, since any fractional value will be rounded on encoding.
- Remember Bencode has no boolean type; represent flags as 0/1 integers if the target consumer expects that convention.
- Cross-check output against a real .torrent file's Bencode structure when building test fixtures for a torrent client.
Developer Notes
Key sorting happens via a plain string comparison over Object.keys() immediately before encoding a dictionary, matching the spec's requirement that keys be compared as raw byte strings; this is what makes the encoder's dictionary output canonical rather than merely valid.
JSON to Bencode Converter Use Cases
- Building a test fixture for a custom BitTorrent client, tracker, or .torrent parser
- Learning how Bencode's four primitives map onto a JSON value's structure
- Debugging a Bencode encoder/decoder implementation by comparing against known-correct output
Common Mistakes
- Assuming key order in the output matches the source JSON; Bencode dictionaries are always emitted in sorted order.
- Using fractional numbers and expecting them to round-trip exactly; Bencode has no float type.
- Expecting a boolean true/false to encode distinctly from an integer 1/0; Bencode has no boolean type.
Tips
- Use Bencode to JSON afterward to confirm the encoding round-trips the way you expect.
- When comparing against a real .torrent file, remember the info dictionary inside it is itself Bencode-encoded and then hashed, not just displayed as text.