A simple JSON ↔ YAML converter CLI tool
Project description
yamljson
A lightweight command-line tool to convert files between JSON and YAML formats.
Table of Contents
- Overview
- Why yamljson?
- Features
- Requirements
- Installation
- Usage
- Programmatic API
- Project Tree
- How It Works
- Error Cases and Notes
- Development
Overview
yamljson is built for fast, simple, local file conversion:
- Convert
.jsonfiles to.yaml - Convert
.yamlfiles to.json - Use a single command with a predictable output filename
This project is ideal for quick data format switching in scripts, dev tooling, and config workflows.
Why yamljson?
yamljson focuses on one thing and does it well:
- Minimal CLI for quick local conversions without heavy setup
- Predictable output naming (
name.json->name.yamland vice versa) - Safe parsing and dumping defaults for day-to-day developer workflows
- File mode and
stdin/stdoutmode for shell pipelines
Features
- Simple CLI command:
convert - Bidirectional conversion: JSON to YAML, YAML to JSON
- UTF-8 file handling
- Human-readable JSON output (
indent=4) - Safe YAML operations (
yaml.safe_load,yaml.safe_dump)
Requirements
- Python
>=3.8 PyYAML
Installation
1. Clone the repository
git clone <your-repository-url>
cd yamljson
2. Install locally
pip install .
This registers the CLI script:
convert
Usage
CLI syntax
convert <filename> --to yaml
convert <filename> --to json
convert --to yaml
convert --to json
Examples
JSON to YAML
convert data.json --to yaml
Output file:
data.yaml
YAML to JSON
convert config.yaml --to json
Output file:
config.json
stdin -> stdout
echo '{"name":"Alice","enabled":true}' | convert --to yaml
Pipeline example
cat file.json | convert --to yaml
Common CLI responses
File not found: <filename>if the input file does not existInvalid option. Use: --to yaml | jsonif--tois missingUnknown target format. Use 'yaml' or 'json'.for unsupported targetsConversion failed: <reason>for parsing and file I/O errors
Programmatic API
You can also use the converter functions directly in Python:
from yamljson.converter import (
ConversionError,
json_string_to_yaml,
json_to_yaml,
yaml_string_to_json,
yaml_to_json,
)
try:
json_to_yaml("data.json", "data.yaml")
yaml_to_json("config.yaml", "config.json")
except ConversionError as exc:
print(f"Conversion failed: {exc}")
String-based usage:
yaml_text = json_string_to_yaml('{"name": "Alice"}')
json_text = yaml_string_to_json("name: Alice\n")
print(yaml_text)
print(json_text)
Project Tree
yamljson/
├── LICENSE
├── README.md
├── pyproject.toml
├── tests/
│ ├── test_cli.py
│ └── test_converter.py
└── yamljson/
├── __init__.py
├── cli.py
└── converter.py
How It Works
yamljson/cli.pyparses CLI arguments.- It validates the conversion target and input mode (file or
stdin). - It routes the operation to:
json_to_yaml(...)oryaml_to_json(...)for file-based usagejson_string_to_yaml(...)oryaml_string_to_json(...)forstdin/stdoutusage
yamljson/converter.pyhandles parsing, conversion, and output serialization.
Error Cases and Notes
- Invalid JSON input raises
ConversionError. - Invalid YAML input raises
ConversionError. - YAML values not representable in JSON raise
ConversionError. - Output files are created in the same directory as the input file.
- Output filename is generated from the input basename:
name.json->name.yamlname.yaml->name.json
- In
stdin/stdoutmode, converted content is written directly to standard output.
Development
Install editable mode for local development:
pip install -e .
Run tests (if/when tests are added/expanded):
python -m pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file yamljson-1.1.0.tar.gz.
File metadata
- Download URL: yamljson-1.1.0.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af4ae1014bb5acc09ce47f9fad0f4ecb7a1abee36d16a02472d87cd94d54f703
|
|
| MD5 |
f09852d57d4a9f631dbc2564a04580e0
|
|
| BLAKE2b-256 |
5324664083cd1a146d613e1307c01f5ee675c192ba0d509f47fccb4d3ca3779c
|
File details
Details for the file yamljson-1.1.0-py3-none-any.whl.
File metadata
- Download URL: yamljson-1.1.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38cd7c85cd0b9d78c5fc33e4b94f1a8365d1415bc1221ca68cfff6a53c056d1a
|
|
| MD5 |
a458af788b0c68dee032bc38262dd339
|
|
| BLAKE2b-256 |
bf2fb4eb6cc77142c2c76add4523b1d3b0a20612b46c131ade5144c70cecc6e5
|