JSON to YAML Converter

Convert JSON to YAML for Kubernetes manifests, GitHub Actions workflows and Docker Compose files. Strings that a YAML parser would read as another type, such as yes, 1.10 or 2024-01-15, are quoted in the output so they stay strings.

Input JSON
Output YAML

1 JSON to YAML example

Input JSON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
    "title": "Awesome Project",
    "version": "1.0.0",
    "dependencies": {
        "react": "^18.0.0",
        "vite": "^4.0.0"
    },
    "scripts": [
        {
            "name": "dev",
            "command": "vite"
        },
        {
            "name": "build",
            "command": "vite build"
        }
    ]
}

YAML Output

1
2
3
4
5
6
7
8
9
10
11
title: Awesome Project
version: 1.0.0
dependencies:
  react: ^18.0.0
  vite: ^4.0.0
scripts:
  - name: dev
    command: vite
  - name: build
    command: vite build

2 Where the output goes

Kubernetes and Docker

Manifests and compose files are YAML, while API output is often JSON. For example, kubectl get -o json prints a resource you can turn back into a manifest here.

CI pipelines

GitHub Actions, GitLab CI and Azure Pipelines all read their workflow definitions from YAML.

Config people have to read

Convert when people will edit the file by hand. YAML allows # comments and drops the braces and quotes, which is often the reason for the switch.

How to use the JSON to YAML tool

  1. Start with JSON in the left editor: paste it, load a file with Upload JSON, or pull it from an endpoint with Load URL. Dummy Data gives you something to play with.
  2. The YAML appears on the right as you type, with two-space indentation and block-style lists.
  3. A parse error names the line that failed, next to the editor label. Fix it and the YAML refreshes.
  4. Click Beautify input when the JSON on the left is hard to read.
  5. Click Copy Result and paste the YAML into your config file. Add comments with # after conversion.

How to convert JSON to YAML in JavaScript, Python and PHP

JavaScript (js-yaml)

const yaml = require("js-yaml");

const raw = '{"name":"api","replicas":3,"version":"1.10","debug":"yes","hosts":["a.example.com","b.example.com"]}';
const data = JSON.parse(raw);

console.log(yaml.dump(data, { indent: 2, lineWidth: -1, noRefs: true }));

Install with npm install js-yaml. lineWidth: -1 turns off folding of long strings, and noRefs stops the dumper emitting &anchor / *alias pairs for repeated objects.

Python (PyYAML)

# pip install pyyaml
import json
import yaml

raw = '{"name":"api","replicas":3,"version":"1.10","debug":"yes"}'
data = json.loads(raw)

print(yaml.safe_dump(data, sort_keys=False, allow_unicode=True, default_flow_style=False))

sort_keys=False matters: PyYAML sorts keys alphabetically unless you tell it not to.

PHP (symfony/yaml)

<?php
// composer require symfony/yaml
require 'vendor/autoload.php';

use Symfony\Component\Yaml\Yaml;

$raw = '{"name":"api","replicas":3,"version":"1.10","debug":"yes"}';
$data = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);

// 2nd arg: nesting level to expand before switching to inline {} syntax
// 3rd arg: indent width
echo Yaml::dump($data, 10, 2);

If Composer is not an option, the PECL yaml extension has yaml_emit() and does the same job.

Common errors and how to fix them

A string value "1.10" comes out as 1.1 after a round trip
The emitter did not quote it, so the YAML parser read it as a float. A correct emitter quotes strings that look like numbers. Check that yours does, or quote them yourself.
The value yes / no / on / off becomes true or false
YAML 1.1 parsers (PyYAML, Go's yaml.v2, older Ruby) treat these words as booleans. Quote them, or use a YAML 1.2 parser such as js-yaml 4 or ruamel.yaml.
Long strings are wrapped onto several lines with >-
Emitters fold strings that exceed a width limit, 80 characters in both js-yaml and PyYAML. Disable it with lineWidth: -1 or width=float("inf"). The folded form parses back to the same string, so this is cosmetic.
Kubernetes rejects the file: "error converting YAML to JSON: line N: found a tab character"
You edited the file and inserted a tab. YAML indentation must be spaces. Set your editor to expand tabs for .yaml files.

Good to know

YAML 1.2 is a superset of JSON, so you could paste the JSON into a .yaml file and stop there. Block style is still worth the conversion, because people can read, diff and comment it. The catch is typing. YAML 1.1 has sexagesimal numbers, so an unquoted 1:30 is 90, and values like 0o17 or 1e3 parse differently depending on the parser version. Quote any string that looks like something other than a string.

Multi-line strings come out as literal block scalars: | keeps the line breaks, and |- also drops the final newline. Trailing spaces inside such a block are part of the string, so an editor that trims whitespace on save changes the value.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Frequently Asked Questions

Does it support comments?

JSON has no comments, so there are none to carry over. Add comments with # after converting; YAML parsers skip them.

Is the output valid for Kubernetes, Docker Compose and GitHub Actions?

Yes. The converter uses js-yaml, which writes two-space indentation and block-style lists that Kubernetes, Docker Compose and GitHub Actions all accept. Valid YAML is not the same as a valid manifest, so check the target tool's schema for required keys.

How are strings that look like numbers or booleans handled?

Strings such as "123", "true", "yes" or "null" are quoted in the output, so a YAML parser keeps them as strings instead of turning them into a number, a boolean or null.

What about multi-line strings?

Strings with line breaks are written as literal block scalars (|), so every line break survives exactly.

Can I convert a large JSON file?

Yes. Upload JSON loads a file from disk, and the conversion runs in your browser tab, so the practical limit is your device's memory rather than an upload cap.

Related

YAML to JSON converts in the other direction. JSON to XML covers older config formats, and the JSON formatter finds the syntax error when the input is rejected.

More Data Converters

Browse all data converters

Copied to clipboard!