json-to-md-table
v1.0.1
Published
Fast, zero-dependency utility and CLI to convert JSON arrays and JavaScript objects into clean, beautifully aligned Markdown tables.
Maintainers
Readme
📊 json-to-md-table
Fast, zero-dependency utility and CLI to convert JSON arrays and JavaScript objects into clean, beautifully aligned Markdown tables.
✨ Features
- ⚡ Zero Dependencies — Ultra-lightweight (~2 KB), pure native TypeScript.
- 📐 Visually Padded & Aligned — Automatically calculates column widths so raw Markdown tables look tidy in code editors and READMEs.
- 🎚️ Column Alignments — Full support for
left(:---),center(:---:), andright(---:) alignments. - 🏷️ Column Filtering & Header Aliases — Select specific fields and rename column titles.
- 🛡️ Auto-Escaping — Automatically escapes pipe characters (
|->\|) and handles multiline text to keep tables valid. - 🧪 Custom Cell Value Formatters — Transform currencies, dates, booleans (
true->✅), or numbers. - 💻 CLI & CI/CD Ready — Pipe JSON directly or run
npx json-to-md-tablein GitHub Actions. - 📦 Dual ESM & CommonJS — Works in Node.js, Next.js, Vite, Express, and browsers.
📥 Installation
As a Library
npm install json-to-md-tableAs a CLI Tool
npx json-to-md-table data.json
# or install globally
npm install -g json-to-md-table🚀 Quick Start (Library)
1. Basic JSON to Markdown Table
import { jsonToMarkdownTable } from 'json-to-md-table';
const users = [
{ id: 1, name: 'Alice', role: 'Admin', active: true },
{ id: 2, name: 'Bob', role: 'Member', active: false },
];
console.log(jsonToMarkdownTable(users));Output:
| id | name | role | active |
| :-- | :---- | :----- | :----- |
| 1 | Alice | Admin | true |
| 2 | Bob | Member | false |2. Custom Headers, Alignment & Formatting
import { jsonToMarkdownTable } from 'json-to-md-table';
const benchmarks = [
{ test: 'Query A', latency: 12.4, status: 'pass' },
{ test: 'Query B', latency: 154.2, status: 'slow' },
];
const table = jsonToMarkdownTable(benchmarks, {
columns: [
{ key: 'test', header: 'Test Suite', align: 'left' },
{
key: 'latency',
header: 'Latency',
align: 'right',
format: (val) => `${val} ms`,
},
{
key: 'status',
header: 'Status',
align: 'center',
format: (val) => (val === 'pass' ? '✅ Pass' : '⚠️ Warning'),
},
],
});
console.log(table);Output:
| Test Suite | Latency | Status |
| :--------- | --------: | :--------: |
| Query A | 12.4 ms | ✅ Pass |
| Query B | 154.2 ms | ⚠️ Warning |3. Convert 2D Arrays
import { twoDArrayToMarkdownTable } from 'json-to-md-table';
const headers = ['Country', 'Capital'];
const rows = [
['France', 'Paris'],
['Japan', 'Tokyo'],
];
console.log(twoDArrayToMarkdownTable(headers, rows));💻 CLI Usage
# Convert a JSON file and print to terminal:
npx json-to-md-table results.json
# Save output to a markdown file:
npx json-to-md-table results.json -o TABLE.md
# Pipe from another command (e.g. curl or jq):
curl -s https://api.example.com/items | npx json-to-md-table⚙️ Options Reference
jsonToMarkdownTable(data, options)| Option | Type | Default | Description |
|---|---|---|---|
| columns | Array<string \| ColumnDefinition> | all keys | Columns to include, their order, and formatting. |
| alignment | 'left' \| 'center' \| 'right' \| Array | 'left' | Global or per-column alignment shorthand. |
| padCells | boolean | true | Pads cells with spaces for visually aligned raw markdown. |
| escapePipes | boolean | true | Escapes \| characters within cell contents. |
| emptyPlaceholder | string | '-' | Text displayed for null or undefined values. |
🛠 Development & Testing
# Install dependencies
npm install
# Run unit tests
npm test
# Build dual ESM/CJS bundles + CLI executable + .d.ts types
npm run build💖 Support the Project
If you find this package useful, consider sponsoring its ongoing development and maintenance: 👉 Sponsor @Piratha97 on GitHub Sponsors
📄 License
MIT © 2026 Pirapaakaran Pirathapan
