Skip to main content
The JSON to YAML tool converts JSON (JavaScript Object Notation) into YAML (YAML Ain’t Markup Language) format. YAML is more human-readable and widely used for configuration files, making this tool essential for creating configs from JSON data.

Use Cases

  • Generating configuration files from JSON API responses
  • Converting JSON to YAML for Kubernetes, Docker Compose, Ansible
  • Creating CI/CD config files (GitHub Actions, GitLab CI, CircleCI)
  • Improving readability of complex JSON structures
  • Converting OpenAPI specs from JSON to YAML format
  • Migrating configs from JSON-based systems to YAML-based systems

How It Works

The tool uses the js-yaml library to serialize JavaScript objects (parsed from JSON) into YAML format:
  1. Parse JSON (with relaxed syntax support via parseJsonLoose)
  2. Convert to YAML using yamlDump() with formatting options:
    • 2-space indentation
    • 120-character line width for readability
  3. Output YAML string
The tool uses a relaxed JSON parser that accepts unquoted keys, single quotes, and trailing commas, making it forgiving for hand-written JSON.

Input Format

Any valid JSON (or relaxed JSON):
{
  "name": "MyApp",
  "version": "1.2.3",
  "config": {
    "debug": true,
    "port": 8080,
    "features": ["auth", "api", "admin"]
  }
}

Output Format

name: MyApp
version: 1.2.3
config:
  debug: true
  port: 8080
  features:
    - auth
    - api
    - admin

Examples

Input:
{
  "app": {
    "name": "MyService",
    "env": "production",
    "debug": false
  }
}

Output:
app:
  name: MyService
  env: production
  debug: false

Technical Details

Located in lib/tools/engine.ts:503-504
The tool uses js-yaml (YAML 1.2 serializer):
import { dump as yamlDump } from 'js-yaml';

const parsed = parseJsonLoose(input);
return { output: yamlDump(parsed, { indent: 2, lineWidth: 120 }) };

Formatting Options

  • Indentation: 2 spaces (standard for YAML)
  • Line width: 120 characters (wraps long strings)
  • Flow level: Auto (uses block style for nested structures)

Type Conversion

JSONYAML
true, falsetrue, false
123, 123.45123, 123.45
nullnull or ~
"string"string (unquoted) or 'string' (quoted if needed)
[1, 2, 3]- 1\n- 2\n- 3 (block style)
{"key": "value"}key: value (block style)
YAML automatically quotes strings that contain special characters (:, #, !, etc.) or reserved words (true, false, null, yes, no, on, off).

YAML Output Features

Unquoted Strings (Default)

Simple strings are unquoted:
name: MyApp
description: A simple application

Quoted Strings (When Needed)

Strings with special characters are automatically quoted:
message: 'Hello: World'  # Colon requires quotes
version: '1.2.3'         # Numeric-looking strings are quoted

Block Style Arrays

Arrays use dash notation:
features:
  - auth
  - api
  - admin

Flow Style (Inline)

Short arrays/objects may use flow style:
ports: ['80:80', '443:443']
env: {NODE_ENV: production, DEBUG: false}

Multi-line Strings

Long strings (>120 chars) are wrapped:
description: >
  This is a very long description that exceeds 120 characters and will be
  automatically wrapped by the YAML serializer for better readability.

Relaxed JSON Parser

The tool accepts non-standard JSON syntax:
// ✅ Accepted (will convert to YAML)
{name: "John", age: 30}        // Unquoted keys
{name: 'John', age: 30}        // Single quotes
{name: "John", age: 30,}       // Trailing comma

Common Patterns

API Response to Config File

  1. Fetch JSON from API:
{"config": {"host": "api.example.com", "timeout": 30}}
  1. Convert to YAML for deployment:
config:
  host: api.example.com
  timeout: 30

Terraform Variables (JSON → YAML)

{"environment": "prod", "instance_type": "t3.medium", "count": 3}
environment: prod
instance_type: t3.medium
count: 3

OpenAPI Spec Conversion

Convert OpenAPI JSON specs to the more common YAML format for better readability:
{"openapi": "3.0.0", "info": {"title": "My API", "version": "1.0.0"}}
openapi: 3.0.0
info:
  title: My API
  version: 1.0.0

Limitations

YAML quotes numeric-looking strings to preserve their type. For example, version numbers like "1.2.3" become '1.2.3' to avoid interpretation as a number. This is correct behavior.
No. JSON does not officially support comments (though JSONC does). When you convert JSON to YAML, any comments in the source are lost. Add comments manually to the YAML output if needed.
on, off, yes, no, true, false are YAML reserved words. When used as keys, they are quoted to prevent misinterpretation as booleans.