How to Parse unrar Output to JSON

This guide explains how to convert output from the command-line utility unrar into structured JSON data. By capturing the text output of the list command and parsing the metadata fields using a scripting language like Python, you can reliably extract file names, sizes, timestamps, and attributes into an automated, machine-readable format.


Step 1: Choose the Best unrar List Command

The default unrar l command formats output as a human-readable table, which can be brittle to parse if filenames contain spaces or special characters.

Instead, use the technical list command:

unrar lt archive.rar

The lt flag displays archive details in key-value pairs separated by colons, making programmatic parsing significantly simpler and less error-prone.

Example snippet of unrar lt output:

Name: documents/report.pdf
Type: File
Size: 1048576
Packed size: 812400
Ratio: 77%
mtime: 2023-10-15 14:32:00
Attributes: ..A....
CRC32: 9A4B8C1E
Host OS: Windows
Compression: RAR 5.0(v50) -m3 -md=32M

Step 2: Parse Output Using Python

Python provides built-in tools (subprocess and json) to run the command, parse the key-value blocks, and export the result as JSON.

Create a script named unrar_to_json.py:

import json
import subprocess
import sys


def parse_unrar_to_json(archive_path):
    # Execute the unrar lt command
    cmd = ["unrar", "lt", archive_path]
    result = subprocess.run(cmd, capture_output=True, text=True, check=True)

    lines = result.stdout.splitlines()
    files = []
    current_entry = {}

    for line in lines:
        line = line.strip()

        # An empty line indicates the end of a file's metadata block
        if not line:
            if current_entry and "Name" in current_entry:
                files.append(current_entry)
                current_entry = {}
            continue

        # Split on the first colon to separate key and value
        if ":" in line:
            key, value = line.split(":", 1)
            key = key.strip()
            value = value.strip()

            # Convert numeric fields where applicable
            if key in ["Size", "Packed size"]:
                try:
                    value = int(value)
                except ValueError:
                    pass

            current_entry[key] = value

    # Append the last block if not followed by a blank line
    if current_entry and "Name" in current_entry:
        files.append(current_entry)

    return json.dumps(files, indent=2)


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python unrar_to_json.py <archive.rar>")
        sys.exit(1)

    archive_file = sys.argv[1]
    try:
        json_output = parse_unrar_to_json(archive_file)
        print(json_output)
    except subprocess.CalledProcessError as e:
        print(f"Error executing unrar: {e.stderr}", file=sys.stderr)
        sys.exit(1)

Step 3: Run the Script

Execute the script by passing your RAR file as an argument:

python unrar_to_json.py sample.rar > output.json

Resulting JSON Format

The script produces a JSON array containing objects for each item inside the archive:

[
  {
    "Name": "documents/report.pdf",
    "Type": "File",
    "Size": 1048576,
    "Packed size": 812400,
    "Ratio": "77%",
    "mtime": "2023-10-15 14:32:00",
    "Attributes": "..A....",
    "CRC32": "9A4B8C1E",
    "Host OS": "Windows",
    "Compression": "RAR 5.0(v50) -m3 -md=32M"
  }
]