All Tools View Categories About Contact Privacy

JSON to Bencode

Encode a JSON data structure into bencode: integers, byte strings with length prefixes, lists and dictionaries with sorted keys. BitTorrent-compatible, all client-side.

JSON Input
Waiting for input
Bencode Output
Objects become dictionaries d...e with byte-wise sorted keys, arrays become lists l...e, strings become length:bytes and integers become i<n>e. Floats are truncated with a warning; booleans encode as i1e / i0e; null uses the non-standard n marker so it survives a round trip.

About JSON to Bencode

Bencode is the plain-text-but-binary-safe format that powers BitTorrent: every .torrent metainfo file, every tracker announce and every DHT message is bencode. When you generate torrent data programmatically, build a tracker client, or need fixtures for a peer implementation, producing correct bencode by hand is fiddly — strings need byte-length prefixes and dictionary keys must be sorted.

Convert JSON to Bencode turns a JSON document into standards-compliant bencode: integers to i<n>e, strings to <len>:<bytes>, arrays to lists, and objects to sorted dictionaries. The output can be pasted into a metainfo file or fed straight to a BitTorrent stack.

Features

  • Integers: JSON numbers become ie, exactly as BitTorrent clients expect.
  • Byte strings: Strings are prefixed with their UTF-8 byte length.
  • Lists: JSON arrays map onto bencode lists l...e.
  • Dictionaries: Objects become d...e with byte-wise sorted keys.
  • Nesting: Lists and dictionaries nest to any depth.
  • Warnings: Floats and special values are flagged instead of silently mangled.
  • Live output: The bencode updates with every keystroke.
  • Private: All encoding happens in your browser.

How to Use

  1. Paste your JSON into the input pane.
  2. Read the bencode — the encoded form appears instantly.
  3. Check the warnings if a float or null was converted.
  4. Copy or download the bencode output.

Examples

Example 1 — Torrent metainfo. A scraper builds a .torrent file: it assembles the info dictionary as JSON (name, piece length, files), converts it to bencode here, and writes the bytes to a file.

Example 2 — Tracker tests. A tracker implementer generates announce and announce-list payloads as JSON fixtures, then converts them to bencode for the test suite.

Example 3 — DHT messages. A P2P developer encodes a dictionary with transaction id, method and arguments as bencode, matching the wire format used by the DHT protocol.

Example 4 — Learning. A student studying BitTorrent pastes sample structures to see exactly how length prefixes and sorted dictionary keys look in practice.

Benefits

  • Correct by construction: Key sorting and length prefixes are handled for you.
  • BitTorrent-compatible: Output follows the metainfo conventions clients actually parse.
  • Fully typed: Numbers, strings, booleans, null and nested structures all map cleanly.
  • Instant: Live encoding while you type.
  • Zero upload: The conversion never leaves your browser.

Frequently Asked Questions

What is bencode?
Bencode is the binary-safe serialization format used by BitTorrent (metainfo files, tracker messages) and several other protocols. It encodes integers as i<num>e, strings as <length>:<bytes>, lists as l...e and dictionaries as d...e.
How are dictionaries encoded?
Dictionaries are encoded as d followed by key/value pairs and a closing e. Keys are byte strings and are sorted in byte-wise order — the encoder sorts object keys automatically, matching what BitTorrent clients expect.
How are strings handled?
Each string is encoded as its UTF-8 byte length, a colon, then the raw bytes: "hello" becomes 5:hello. UTF-8 multi-byte characters are counted by byte length, not character count.
What happens to floats and booleans?
Bencode only supports integers, so a float is truncated to an integer and a warning is shown. Booleans follow the common convention of encoding to i1e (true) or i0e (false) — the companion decoder reads them back as integers 1 and 0.
What happens to null values?
Bencode has no null type. This tool encodes null as the non-standard n marker so the data survives a round trip through the companion bencode-to-json decoder, which maps n back to null.
Is my JSON uploaded anywhere?
No. Encoding happens entirely in your browser. Nothing is uploaded, stored or logged.