Guide
Back Up Email to JSON from Terminal
Export your entire mailbox to structured JSON files from the command line. Paginate through all messages across Gmail, Outlook, Exchange, Yahoo, iCloud, and IMAP. Filter by date range, folder, or sender. Store backups locally or pipe to cloud storage.
Written by Aaron de Mello Senior Engineering Manager
Why back up email to JSON?
Email backup as JSON gives you a portable, queryable archive that isn't locked inside any provider's ecosystem. The Radicati Group's Email Statistics Report 2024-2028 estimated the average business user sends and receives 126 emails per day. Over a year, that's roughly 46,000 messages — and most backup tools produce opaque mbox or PST files that require specialized software to read. JSON is human-readable, grep-friendly, and parseable by every programming language.
Traditional email backup methods have sharp limitations. Google Takeout exports once per request and takes hours for large mailboxes. Outlook PST exports require the desktop client. IMAP tools like offlineimap need server configuration and don't produce structured output. The Nylas CLI exports to JSON in a single command with automatic pagination across all 6 providers.
How do you install the CLI?
The Nylas CLI installs as a single binary with no runtime dependencies. Homebrew resolves the correct binary for your architecture in under 10 seconds. macOS (Intel and Apple Silicon), Linux, and Windows (via WSL or PowerShell) are supported. Authentication stores your API key locally in ~/.config/nylas/credentials.json.
brew install nylas/nylas-cli/nylasFor shell-script, PowerShell, and Go installs, see the getting started guide.
How do you export your entire inbox?
The nylas email list command with --all paginates through every message in a folder and returns them as a single JSON array. By default it reads from INBOX. The CLI handles pagination tokens automatically — you don't need to manage page cursors or retry failed pages. A mailbox with 10,000 messages typically exports in 2-3 minutes depending on your connection speed and the provider's rate limits.
# Export entire inbox to a JSON file
nylas email list --all --json > inbox-backup.json
# Cap the export at 5,000 messages
nylas email list --all --max 5000 --json > inbox-recent.json
# Check how many messages were exported
jq length inbox-backup.jsonHow do you filter by date range or folder?
Large mailboxes benefit from incremental backups. The nylas email search command accepts --after and --before flags for date filtering, returning only messages within that window. Combined with --folder, you can target specific folders like SENT, DRAFTS, or custom labels. A monthly incremental backup of a 50-email-per-day inbox completes in under 30 seconds.
# Back up only May 2026 messages
nylas email search "*" --after 2026-05-01 --before 2026-05-31 --json > may-2026.json
# Back up sent folder
nylas email list --all --folder SENT --json > sent-backup.json
# Back up from a specific sender
nylas email search "*" --from "ceo@company.com" --json > ceo-emails.jsonHow do you back up all folders at once?
The --all-folders flag bypasses the default INBOX filter and exports messages from every folder in the mailbox. Gmail organizes mail into labels (INBOX, SENT, DRAFTS, SPAM, TRASH, and user-created labels). Outlook uses folders (Inbox, Sent Items, Drafts, Junk Email, and custom folders). The CLI normalizes both systems into a single JSON structure with a folders field on each message.
# Export every message from every folder
nylas email list --all --all-folders --json > full-backup.json
# Count messages per folder with jq
jq '[.[].folders[0]] | group_by(.) | map({folder: .[0], count: length})' full-backup.jsonHow do you automate daily backups with cron?
A cron job can run incremental backups every night, appending only new messages since the last run. The script below uses the previous backup's latest date as the --after filter, creating a rolling archive that never re-downloads old messages. Each daily increment for a typical 50-email-per-day mailbox produces a 200-500 KB JSON file.
#!/bin/bash
# daily-email-backup.sh — run via cron at midnight
BACKUP_DIR="$HOME/email-backups"
mkdir -p "$BACKUP_DIR"
DATE=$(date +%Y-%m-%d)
# Yesterday's date for the --after filter
YESTERDAY=$(date -v-1d +%Y-%m-%d 2>/dev/null || date -d "yesterday" +%Y-%m-%d)
nylas email search "*" \
--after "$YESTERDAY" \
--json > "$BACKUP_DIR/backup-$DATE.json"
echo "Backed up $(jq length "$BACKUP_DIR/backup-$DATE.json") messages"Add the script to your crontab with crontab -e and schedule it for midnight. The CLI uses credentials from ~/.config/nylas/credentials.json so no password prompts interrupt the job.
# Run at midnight every day
0 0 * * * /bin/bash $HOME/daily-email-backup.sh >> $HOME/email-backups/cron.log 2>&1How do you query backed-up emails?
JSON backups are immediately queryable with jq. Unlike mbox files (which need formail or mu to parse) or PST files (which need Outlook or readpst), JSON works with every command-line tool and programming language. The structured format includes subject, sender, recipients, date, body, and folder metadata — no MIME decoding required.
# Find all emails from a specific domain
jq '[.[] | select(.from[0].email | test("@company\.com"))]' backup.json
# List subjects and dates, sorted by date
jq -r '.[] | [.date, .subject] | @tsv' backup.json | sort
# Count emails by sender
jq -r '.[].from[0].email' backup.json | sort | uniq -c | sort -rn | head -20
# Extract all attachment filenames
jq -r '[.[] | .attachments[]? | .filename] | .[]' backup.jsonNext steps
Install the CLI and run your first backup with the getting started guide. To search exported JSON more efficiently, see search email from terminal. For automating email reports from backed-up data, check automate email reports. The full command reference covers all email list and email search flags.