Zero-config CLI for converting markdown to a JSON-safe string. Pass a file or pipe stdin, get a properly escaped JSON string on stdout. No flags required.
Ensure mdstr is available:
bash
# Check if installed
which mdstr
# Install globally if needed
npm install -g mdstr
# Or use npx for one-off conversions
npx mdstr <file>
Requires Node.js 20 or later.
bash
mdstr README.md
mdstr ./docs/guide.md
bash
cat notes.md | mdstr
echo '# "Hello" World' | mdstr
# → "# \"Hello\" World"
The output is always a single JSON string on stdout, surrounded by quotes, with all special characters properly escaped. Ready to embed directly into JSON structures.
FlagDescription--preserve-newlineKeep trailing newline in output (stripped by default)--versionShow version number--helpShow help with examples
CodeMeaning0Success1Read/conversion error2Invalid usage (file not found, no input)
bash
mdstr instructions.md
# → "# Instructions\n\nDo this and that.\n\nSaid \"hello\" and left."
Use jq to build JSON structures with markdown content:
bash
jq -n --argjson content "$(mdstr instructions.md)" '{prompt: $content}' > payload.json
bash
SYSTEM_PROMPT=$(mdstr system-prompt.md)
curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "$(jq -n --argjson prompt "$SYSTEM_PROMPT" '{
model: "gpt-4o",
messages: [{role: "system", content: $prompt}]
}')"
bash
jq -n \
--argjson system "$(mdstr system.md)" \
--argjson user "$(mdstr user-prompt.md)" \
'{messages: [{role: "system", content: $system}, {role: "user", content: $user}]}'
bash
echo '- line 1
- line 2' | mdstr | jq '{content: .}'
# → {"content": "- line 1\n- line 2"}
By default, mdstr strips the trailing newline. To preserve it:
bash
mdstr README.md --preserve-newline
The output is always:
", \, newlines, tabs, etc.)--preserve-newline to keep)No confirmation prompts. No color codes. No spinners. Designed for automated pipelines and agent tool calls.
When conversion fails, check:
Errors are printed to stderr with actionable context.
Convert markdown documentation or prompts into JSON-safe strings for REST API calls:
bash
mdstr prompt.md
# Use the output directly in curl or httpie commands
Convert system prompts, user messages, or few-shot examples stored as markdown files into strings suitable for LLM API payloads.
Embed markdown content (descriptions, help text, documentation) into JSON config files.
Convert release notes or changelogs from markdown to JSON-safe strings for automated notifications or API integrations.