Skip to content

Simple json to yaml and yaml to json converter via CLI

License

Notifications You must be signed in to change notification settings

willyanibe/jsonyaml-cli

Repository files navigation


# jsonyaml-cli

Lightweight, no-nonsense CLI to convert **JSON ⇄ YAML**.

## Features

- Auto-detects input format (by extension or content).
- Converts JSON ⇄ YAML with sane defaults.
- Supports:
  - Multi-document YAML
  - JSON Lines / NDJSON
  - Strict mode for duplicate YAML keys
- UTF-8 safe with PowerShell notes below.

## Install

Editable install for local dev:

```powershell
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -e ".[test]"

Check version:

jsonyaml --version

Usage

Convert JSON → YAML

jsonyaml -i .\data.json -o .\data.yaml

Convert YAML → JSON

jsonyaml -i .\data.yaml -o .\data.json

Multi-doc YAML → JSON array

jsonyaml -i .\multi.yml --from yaml --to json --multi array -o .\multi.json

JSON Lines → multi-doc YAML

jsonyaml -i .\logs.ndjson --from json --json-lines --to yaml -o .\logs.yaml

Pipe examples

'{"hello":"world","nums":[1,2,3]}' | jsonyaml --from json --to yaml

Options

I/O
  -i, --in, --input PATH         Input path (default: stdin, use "-")
  -o, --out, --output PATH       Output path (default: stdout)

Direction
  --from {json,yaml}             Override input format
  --to   {json,yaml}             Desired output format

YAML
  --yaml-indent INT              Indent (default: 2)
  --yaml-flow                    Use flow style
  --yaml-sort-keys               Sort keys
  --yaml-doc-start               Prefix docs with '---'
  --yaml-strict-dupes            Error on duplicate keys during YAML load

JSON
  --json-indent INT              Indent (default: 2)
  --json-sort-keys               Sort keys
  --json-ensure-ascii            Escape non-ASCII (default: UTF-8)
  --json-lines                   Treat input as JSON Lines / NDJSON
  --multi {array,lines,first}    YAML→JSON multi-doc strategy (default: array)

General
  --strict                       Nonzero exit on empty input, etc.
  -q, --quiet                    Suppress diagnostics
  --version                      Show version

PowerShell notes (Windows)

  • PS 5.x writes UTF-8 files with a BOM by default, which can cause Unexpected UTF-8 BOM parse errors. Fixes:

    • Use PS 7+ with -Encoding utf8NoBOM, or

    • Re-save without BOM:

      $txt = Get-Content .\data.json -Raw -Encoding UTF8
      [IO.File]::WriteAllText(".\data.json", $txt, (New-Object System.Text.UTF8Encoding($false)))
    • Or pipe instead of using -i/-o:

      Get-Content .\data.json -Raw -Encoding UTF8 | jsonyaml --from json --to yaml
  • Always quote paths with spaces, e.g.:

    jsonyaml -i "C:\Users\Wilson\Documents\my data\input.json" -o .\out.yaml

Development & Tests

Run the test suite:

pytest -q

License

MIT


About

Simple json to yaml and yaml to json converter via CLI

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages