Skip to main content

A simple JSON ↔ YAML converter CLI tool

Project description

yamljson

Python PyPI Version PyPI Downloads Test Status Coverage CLI Dependency Format

A lightweight command-line tool to convert files between JSON and YAML formats.

Table of Contents

Overview

yamljson is built for fast, simple, local file conversion:

  • Convert .json files to .yaml
  • Convert .yaml files 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.yaml and vice versa)
  • Safe parsing and dumping defaults for day-to-day developer workflows
  • File mode and stdin/stdout mode 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 exist
  • Invalid option. Use: --to yaml | json if --to is missing
  • Unknown target format. Use 'yaml' or 'json'. for unsupported targets
  • Conversion 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

  1. yamljson/cli.py parses CLI arguments.
  2. It validates the conversion target and input mode (file or stdin).
  3. It routes the operation to:
    • json_to_yaml(...) or
    • yaml_to_json(...) for file-based usage
    • json_string_to_yaml(...) or
    • yaml_string_to_json(...) for stdin/stdout usage
  4. yamljson/converter.py handles 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.yaml
    • name.yaml -> name.json
  • In stdin/stdout mode, 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yamljson-1.1.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yamljson-1.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

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

Hashes for yamljson-1.1.0.tar.gz
Algorithm Hash digest
SHA256 af4ae1014bb5acc09ce47f9fad0f4ecb7a1abee36d16a02472d87cd94d54f703
MD5 f09852d57d4a9f631dbc2564a04580e0
BLAKE2b-256 5324664083cd1a146d613e1307c01f5ee675c192ba0d509f47fccb4d3ca3779c

See more details on using hashes here.

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

Hashes for yamljson-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 38cd7c85cd0b9d78c5fc33e4b94f1a8365d1415bc1221ca68cfff6a53c056d1a
MD5 a458af788b0c68dee032bc38262dd339
BLAKE2b-256 bf2fb4eb6cc77142c2c76add4523b1d3b0a20612b46c131ade5144c70cecc6e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page