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.
{
"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"
}
]
} 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
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.
GitHub Actions, GitLab CI and Azure Pipelines all read their workflow definitions from YAML.
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.
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.
# 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
// 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.
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.
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.
JSON has no comments, so there are none to carry over. Add comments with # after converting; YAML parsers skip them.
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.
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.
Strings with line breaks are written as literal block scalars (|), so every line break survives exactly.
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.
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.