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.
id: 1
name: John Doe
role: Admin
active: true
skills:
- JavaScript
- Python
- Go {
"id": 1,
"name": "John Doe",
"role": "Admin",
"active": true,
"skills": [
"JavaScript",
"Python",
"Go"
]
} Many validators, APIs and schema checkers accept JSON only, while the file you maintain is YAML.
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.
const 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().
# 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
// 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.
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.
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.
Mappings, sequences, scalars, multi-line strings, anchors and aliases all work. Anchors are resolved, so the JSON contains the expanded values.
They are dropped. JSON has no comment syntax, so there is nowhere to put them.
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.
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.
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.
JSON to YAML converts in the other direction, and the JSON formatter checks the result before you paste it somewhere.