YAML to JSON Converter
Convert YAML to JSON to hand a config file to a tool that only accepts JSON. The output also shows exactly how the YAML parsed, which is the quickest way to find an indentation mistake two levels up.
1 YAML to JSON example
Input YAML
2
3
4
5
6
7
8
id: 1
name: John Doe
role: Admin
active: true
skills:
- JavaScript
- Python
- Go Output JSON
2
3
4
5
6
7
8
9
10
11
{
"id": 1,
"name": "John Doe",
"role": "Admin",
"active": true,
"skills": [
"JavaScript",
"Python",
"Go"
]
} 2 Where YAML to JSON comes up
Feeding a JSON-only tool
Many validators, APIs and schema checkers accept JSON only, while the file you maintain is YAML.
Seeing what the YAML parsed to
Convert the file when a value behaves oddly. In plain JSON you can see resolved anchors, multi-line strings and typed scalars, such as a version 1.10 that parsed as the number 1.1.
Usage
Resolving YAML anchors and keys into JSON
- 01 Paste YAML into the left editor, or load it with Upload YAML or Load URL. Dummy Data provides a sample.
- 02 JSON appears on the right, two-space indented. Anchors and aliases are resolved into plain values, and comments are gone, since JSON has no place for them.
- 03 When parsing fails, the error appears next to the editor label. The cause is usually a tab used for indentation or a colon inside an unquoted value.
- 04 Click Copy JSON to put the result on the clipboard.
In code
How to convert YAML to JSON in JavaScript, Python and PHP
JavaScript (js-yaml)
jsconst yaml = require("js-yaml");
const text = `
defaults: &defaults
adapter: postgres
port: 5432
development:
<<: *defaults
database: app_dev
debug: yes
`;
const data = yaml.load(text); // safe by default in js-yaml 4
console.log(JSON.stringify(data, null, 2)); js-yaml 4 follows YAML 1.2, so yes stays a string here. For files with several --- documents use yaml.loadAll().
Python (PyYAML)
python# pip install pyyaml
import json
import yaml
text = """
defaults: &defaults
adapter: postgres
development:
<<: *defaults
database: app_dev
debug: yes
"""
data = yaml.safe_load(text) # never use yaml.load() on untrusted input
print(json.dumps(data, indent=2)) PyYAML still follows YAML 1.1, so debug: yes becomes true. If that bites you, ruamel.yaml implements 1.2.
PHP (symfony/yaml)
php<?php
// composer require symfony/yaml
require 'vendor/autoload.php';
use Symfony\Component\Yaml\Yaml;
$text = "development:\n adapter: postgres\n port: 5432\n debug: yes\n";
$data = Yaml::parse($text);
echo json_encode($data, JSON_PRETTY_PRINT);
// With the PECL yaml extension instead: json_encode(yaml_parse($text)); Symfony reads yes as a string. The PECL extension turns it into a boolean by default, so the two disagree on the same file.
Debugging
Tab indentation and duplicate key failures
- found character '\t' that cannot start any token
- A tab was used for indentation. YAML only allows spaces. Replace tabs with two spaces; most editors can show whitespace to find them.
- mapping values are not allowed in this context
- An unquoted value contains a colon followed by a space, for example title: Note: draft. Wrap the value in quotes.
- The value 1.10 becomes 1.1 and 0755 becomes 493
- Unquoted scalars are typed by the parser: 1.10 is a float and 0755 is an octal integer in YAML 1.1. Quote version numbers and file modes.
- Norway problem: country: NO becomes false
- YAML 1.1 reads NO, No, off and n as booleans. Quote the value, or move to a YAML 1.2 parser.
Edge cases
How YAML types coerce into JSON
YAML 1.2 came out in 2009 and removed the yes/no/on/off booleans, sexagesimal numbers and most of the other implicit typing surprises. Many libraries still implement 1.1: PyYAML, the PECL yaml extension and Go's yaml.v2. js-yaml 4, ruamel.yaml, Go's yaml.v3 and Symfony follow 1.2. The same file can therefore mean different things in different tools. This converter uses js-yaml 4, and the snippet notes above say which version each library follows.
Never feed untrusted YAML to a loader that constructs arbitrary objects. PyYAML's yaml.load() with the unsafe Loader (or with no Loader argument before version 5.1) honors a !!python/object tag, and that means running code. Use safe_load(). js-yaml's default schema is already safe.
Standards
YAML 1.2 specification and JSON RFC
Q&A
YAML to JSON questions
Which YAML features are supported?
Mappings, sequences, scalars, multi-line strings, anchors and aliases all work. Anchors are resolved, so the JSON contains the expanded values.
What happens to comments?
They are dropped. JSON has no comment syntax, so there is nowhere to put them.
Can I convert a file with multiple documents?
Not in one go. The parser reads a single YAML document, so split a multi-document file at each --- separator and convert the parts one at a time.
Why does "yes" become true?
It does not here. This converter uses js-yaml 4, which follows YAML 1.2, so yes, no, on and off stay strings. YAML 1.1 parsers such as PyYAML and Go's yaml.v2 read them as booleans, so quote those values if another tool reads the same file.
Is indentation important?
Yes. YAML uses indentation for structure and does not allow tabs in it. If you see a parse error, check the line it names for a tab or an indentation width that does not match its siblings.