all2md.parsers.json
JSON to AST converter.
This module provides intelligent conversion from JSON structures to readable document format. By default, it transforms the structure into a hierarchical document with headings, tables, and lists. Alternatively, it can render JSON as a literal code block (via the literal_block option).
Examples
Smart Conversion (default):
Input JSON:
{
"server": {"host": "localhost", "port": 8080},
"users": [
{"name": "Alice", "role": "admin"},
{"name": "Bob", "role": "user"}
]
}
Output (as markdown):
# server
* **host**: localhost
* **port**: 8,080
# users
| name | role |
|------|------|
| Alice | admin |
| Bob | user |
Literal Block Mode:
Input JSON (same as above)
Output (as markdown):
{
"server": {
"host": "localhost",
"port": 8080
},
"users": [
{"name": "Alice", "role": "admin"},
{"name": "Bob", "role": "user"}
]
}
- class all2md.parsers.json.JsonParser
Bases:
BaseParserConvert JSON structures to AST representation.
This parser creates a readable document from JSON by converting: - Objects/dicts → heading hierarchies - Arrays of objects → tables - Arrays of primitives → lists - Nested structures → subsections
- Parameters:
options (JsonParserOptions or None, default = None) – Parser configuration options
progress_callback (ProgressCallback or None, default = None) – Optional callback for progress updates
Examples
Basic parsing:
>>> parser = JsonParser() >>> doc = parser.parse('{"name": "John", "age": 30}')
With options:
>>> options = JsonParserOptions(max_heading_depth=3, sort_keys=True) >>> parser = JsonParser(options) >>> doc = parser.parse(json_file_path)
Initialize the JSON parser with options and progress callback.
- __init__(options: JsonParserOptions | None = None, progress_callback: Callable[[ProgressEvent], None] | None = None)
Initialize the JSON parser with options and progress callback.
- parse(input_data: str | Path | IO[bytes] | bytes) Document
Parse JSON input into AST Document.
- Parameters:
input_data (str, Path, IO[bytes], or bytes) – JSON input to parse. Can be: - File path (str or Path) - File-like object in binary mode - Raw JSON bytes - JSON string
- Returns:
AST document node
- Return type:
- Raises:
ParsingError – If parsing fails
- extract_metadata(data: Any) DocumentMetadata
Extract metadata from JSON structure.
- Parameters:
data (Any) – JSON data structure
- Returns:
Extracted metadata
- Return type: