pyproject.toml (Python) to JSON Conversion
Convert Python's pyproject.toml to JSON format. Covers build system configuration, project metadata, tool settings for Black, Ruff, pytest, and dependency specification.
Detailed Explanation
pyproject.toml is the standard configuration file for modern Python projects, defined in PEP 518 and PEP 621. It consolidates build system configuration, project metadata, and tool settings into a single TOML file.
A typical pyproject.toml:
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "my-package"
version = "1.0.0"
description = "A sample Python package"
readme = "README.md"
requires-python = ">=3.9"
license = {text = "MIT"}
authors = [
{name = "Alice", email = "alice@example.com"},
]
dependencies = [
"requests>=2.28",
"pydantic>=2.0",
]
[project.optional-dependencies]
dev = ["pytest>=7.0", "ruff>=0.1"]
[tool.ruff]
line-length = 88
target-version = "py39"
[tool.ruff.lint]
select = ["E", "F", "I"]
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = "-v --tb=short"
Converts to JSON:
{
"build-system": {
"requires": ["hatchling"],
"build-backend": "hatchling.build"
},
"project": {
"name": "my-package",
"version": "1.0.0",
"description": "A sample Python package",
"readme": "README.md",
"requires-python": ">=3.9",
"license": {"text": "MIT"},
"authors": [
{"name": "Alice", "email": "alice@example.com"}
],
"dependencies": [
"requests>=2.28",
"pydantic>=2.0"
]
},
"project.optional-dependencies": {
"dev": ["pytest>=7.0", "ruff>=0.1"]
},
"tool": {
"ruff": {
"line-length": 88,
"target-version": "py39",
"lint": {
"select": ["E", "F", "I"]
}
},
"pytest": {
"ini_options": {
"testpaths": ["tests"],
"addopts": "-v --tb=short"
}
}
}
}
Notable conversion details:
[tool.*]sections create deeply nested JSON under thetoolkey. Each tool (ruff, pytest, mypy) gets its own namespace.- Authors use the array of tables pattern with inline tables.
- Optional dependencies map group names to arrays of PEP 508 requirement strings.
- Dotted table headers like
[tool.ruff.lint]produce nested JSON objects:tool -> ruff -> lint.
The JSON representation is useful for programmatic analysis of Python project configurations, dependency resolution tools, and CI/CD systems that need to extract metadata.
Use Case
Building a Python monorepo management tool that reads pyproject.toml from multiple packages, converts them to JSON, and generates a unified dependency graph with version conflict detection.