JSON ↔ YAML Converter
Convert between JSON and YAML formats with ease.
JSON ↔ YAML Converter translates between the two configuration formats without pulling in js-yaml or any other dependency — the lib header says it outright: "pure string operations, no external YAML library". The mode button reads "JSON → YAML" (default) or "YAML → JSON"; Swap flips direction and pipes the current output back in for chained conversions. JSON→YAML parses with JSON.parse and walks the tree; YAML→JSON runs a line-based indent-stack parser that handles top-level lists, quoted strings, null/~, booleans, ints, and floats.
Output is 2-space-indented YAML or pretty JSON (JSON.stringify(..., null, 2)), both copy-ready from the output header. Strings that would be ambiguous in YAML — empty, containing : or #, numeric-looking, or true|false|null|yes|no — are auto-quoted on emit. Comments in YAML input are skipped, not preserved. Failures surface as mode-specific toasts ("Invalid JSON input." / "Invalid YAML input."). Everything is local.
How it works
jsonToYaml in lib/jsonYaml.ts: JSON.parse then a recursive serializer — arrays become "- " items (object items inline their first key), objects become key: value with 2-space indent(level) padding, {} / [] render empty, and strings are quoted when they contain : or #, have edge spaces/newlines, look numeric, or match true|false|null|yes|no (case checks per the quoting rules).
yamlToJson scans lines with an indent stack, detects top-level lists by a leading "- ", skips blank lines and # comments, and classifies scalars in parseYamlValue (null/~, booleans, ints, floats, quoted strings). The page converts on button click; Swap moves output→input. Empty input toasts "Please enter input to convert"; parse errors show the mode-specific invalid toast. No network anywhere.
How to use
- Mode defaults to JSON→YAML (the action button reads "JSON → YAML").
- Paste JSON into "JSON Input" (placeholder shows a { "key": "value" } object example).
- Click the convert button — toast "Converted JSON to YAML!" or "Invalid JSON input." on failure.
- Read "YAML Output" and Copy via the icon (toast "Copied to clipboard!").
- Click "Swap" to flip direction — output becomes the new input.
- In YAML→JSON mode the placeholder changes to "key: value"; convert produces pretty JSON (2-space indent).
- "Clear" resets input/output.
Tips
- Strings with :, #, or looking numeric/boolean are auto-quoted in YAML output — safe for round-trips of those values.
- Comments in YAML input are skipped by the parser (line.trim().startsWith('#')) and never appear in the JSON output.
- Top-level YAML arrays (first line starting with - ) are detected and parsed as JSON arrays.
- Swap moves output into input so you can chain conversions (JSON → YAML → back) without copy-paste.
- This serializer covers common config shapes, not the full YAML spec (no anchors/aliases/multi-doc) — verify exotic files.
JSON ↔ YAML Converter FAQ
Which YAML library is used?
None — the file header states "pure string operations, no external YAML library"; jsonToYaml/yamlToJson are implemented in-repo.
Are YAML comments preserved?
No — they're skipped during parse and not re-emitted in JSON output.
What indentation is used?
2 spaces per level via the indent(level) helper in lib/jsonYaml.ts.
Why "Invalid YAML input."?
yamlToJson or JSON.parse threw; the catch shows the toast matching the current mode's source format.
Is anything uploaded?
No — both directions are pure string transforms running locally in the browser.
Credits
- In-repo YAML serializer/parser — jsonToYaml/yamlToJson implemented without js-yaml in lib/jsonYaml.ts