docstring-json is a Python parser for JSON with comments and docstrings.
It lets you keep human-friendly documentation directly inside JSON-like files while still loading data into normal Python dictionaries and lists.
Standard JSON is strict and does not allow comments or multiline doc-style strings. docstring-json extends JSON with practical authoring features:
- JavaScript-style comments (
//and/* ... */) - Bare keys (unquoted identifiers)
- Triple-quoted docstrings (
""" ... """) for multiline text
After preprocessing, the content is parsed with Python’s built-in json parser.
pip install docstring-jsonimport docstring_json
text = """
{
// Display settings
title: "Campaign Handbook",
description: """
A player-facing handbook.
Includes lore, rules, and examples.
""",
page_count: 128
}
"""
data = docstring_json.loads(text)
print(data["title"])
print(data["description"])import docstring_json
data = docstring_json.load("config.djson")
print(data)import docstring_json
data = {
"name": "Field Manual",
"notes": "This is a long block of descriptive text.",
}
docstring_json.dump(data, "output.djson")- Comments are removed before JSON parsing.
- Triple-quoted values are converted into JSON strings.
- By default, line breaks inside triple-quoted blocks are preserved.
- Use
collapse_whitespace=Trueto normalize all whitespace into single spaces.
import docstring_json
value = docstring_json.loads(
'{ text: """line 1\nline 2\nline 3""" }',
)
print(value["text"])docstring-json raises custom parse errors for malformed djson syntax such as uneven or unclosed triple-quote delimiters.
import docstring_json
try:
docstring_json.loads('{ bad: """"" }')
except docstring_json.DjsonParseError as exc:
print(f"Invalid djson: {exc}")For standard JSON syntax problems (for example trailing commas), Python’s json.JSONDecodeError is raised.
docstring_json.loads(text, *, collapse_whitespace=False, **kwargs)docstring_json.load(path, *, collapse_whitespace=False, encoding="utf-8", **kwargs)docstring_json.dumps(data, *, indent=2, threshold=80)docstring_json.dump(data, path, *, indent=2, threshold=80, encoding="utf-8")
- Config files with inline explanations
- Content databases with rich multiline descriptions
- Human-authored JSON-like files that need documentation context
MIT