A clean and efficient Python script to split OpenAPI JSON specifications into smaller, manageable chunks based on endpoint tags. Each chunk is saved as a separate JSON file in a dedicated output directory.
- Tag-based splitting: Groups endpoints by their OpenAPI tags
- Dual input support: Works with both local files and remote URLs
- Clean output: Removes empty sections for cleaner JSON files
- Safe filenames: Converts tag names to filesystem names
- Network support: Handles HTTP/HTTPS URLs with timeout and error recovery
- Comprehensive error handling: Validates input and provides helpful error messages
- Modern Python: Uses type hints and modern Python features
# Clone or download the project
cd openchunks
# Install with uv
uv sync# Install from source
pip install .# Run with local file
python main.py path/to/your/openapi-spec.json
# Run with remote URL
python main.py https://api.example.com/openapi.json
# Run with localhost URL
python main.py http://localhost:3000/swagger.json# Local file
python main.py petstore-api.json
# Remote API documentation
python main.py https://petstore.swagger.io/v2/swagger.json
# Local development server
python main.py http://localhost:8080/api-docsThis will create a json_chunks directory with files like:
pets.json- All endpoints tagged with "pets"users.json- All endpoints tagged with "users"store.json- All endpoints tagged with "store"untagged.json- Endpoints without any tags
The script analyzes your OpenAPI specification and:
- Reads the JSON specification file
- Groups operations by their tags
- Creates separate JSON files for each tag
- Cleans empty sections from each chunk
- Saves files with safe names in the
json_chunksdirectory
- Tagged endpoints: Grouped by their tag names
- Untagged endpoints: Placed in an
untagged.jsonfile - Multiple tags: Operations with multiple tags appear in multiple chunks
- Safe names: Tag names are sanitized for filesystem compatibility
Each generated JSON file contains a complete OpenAPI specification with:
{
"openapi": "3.0.0",
"info": { ... },
"servers": [ ... ],
"paths": {
"/api/v1/pets": {
"get": { ... },
"post": { ... }
}
},
"components": { ... },
"tags": [ ... ]
}The script provides helpful error messages for common issues:
- File not found: Clear indication if the input file doesn't exist
- Invalid JSON: Points out JSON parsing errors
- Missing paths: Validates that the spec contains endpoint definitions
- Write errors: Reports issues when saving chunk files
- Python 3.8 or higher
- requests library (automatically installed via pyproject.toml)
# Using uv
uv run python -m pytest
# Or directly
python -m pytest# Format code
uv run black .
# Lint code
uv run flake8 .
# Type checking
uv run mypy .For testing, you can use this minimal example:
{
"openapi": "3.0.0",
"info": {
"title": "Test API",
"version": "1.0.0"
},
"paths": {
"/pets": {
"get": {
"tags": ["pets"],
"summary": "Get all pets",
"responses": {
"200": {
"description": "Success"
}
}
},
"post": {
"tags": ["pets"],
"summary": "Create a pet",
"responses": {
"201": {
"description": "Created"
}
}
}
},
"/users": {
"get": {
"tags": ["users"],
"summary": "Get all users",
"responses": {
"200": {
"description": "Success"
}
}
}
}
}
}MIT License - see LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
If you encounter any issues or have questions:
- Check the error messages for specific guidance
- Ensure your OpenAPI spec is valid JSON
- Verify that your spec contains a
pathssection - Make sure endpoint operations have proper
tagsdefined