markdown-to-json
raw JSON → 2.1.2 verified Mon Apr 27 auth: no python
A Python library for converting Markdown content into structured dictionaries or JSON, commonly used for extracting headings, paragraphs, and metadata. Current version 2.1.2, supports Python >=3.8, maintained by njvack on GitHub.
pip install markdown-to-json Common errors
error AttributeError: module 'markdown_to_json' has no attribute 'markdown_to_json' ↓
cause Incorrect import path: Some users try to import the function directly instead of the module.
fix
Use
import markdown_to_json then call markdown_to_json.markdown_to_json(). error TypeError: expected string or bytes-like object ↓
cause Passing a non-string (e.g., a file object) to markdown_to_json.
fix
Read the Markdown file into a string first:
with open('file.md') as f: text = f.read(). Warnings
breaking Version 2.0 changed the output format: It no longer returns a JSON string; markdown_to_json.markdown_to_json returns a Python dict. Use json.dumps() if you need a string. ↓
fix Update code that expects a JSON string to handle a dict, or call json.dumps() on the result.
gotcha The library only preserves the first paragraph under each heading; nested content may be lost or flattened. ↓
fix Ensure your Markdown structure uses one paragraph per heading if you need full capture.
Imports
- markdown_to_json wrong
from markdown_to_json import markdown_to_jsoncorrectimport markdown_to_json
Quickstart
import markdown_to_json
markdown_text = """# Hello World
This is a paragraph.
"""
# Convert to dict
result = markdown_to_json.markdown_to_json(markdown_text)
print(result)
# Output: {'Hello World': 'This is a paragraph.'}