Convert YAML to JSON for tools that only speak JSON
YAML is the format people prefer to write, and JSON is the format most APIs, databases and libraries accept. This YAML to JSON converter parses your document and writes the same data as JSON, so you can send a Kubernetes manifest to an API, load an OpenAPI description into a tool that wants JSON, or turn YAML test fixtures into request bodies.
Example
server:
host: 0.0.0.0
port: 8080
debug: off
allowed: [web, api]
motd: |
Welcome
back
timeout: 30s
Converted with a 2-space indent:
{
"server": {
"host": "0.0.0.0",
"port": 8080,
"debug": "off"
},
"allowed": [
"web",
"api"
],
"motd": "Welcome\nback\n",
"timeout": "30s"
}
Three details are worth noticing: port became a number, debug: off stayed the string "off", and the literal block | kept its line breaks, including the final newline, as \n escapes.
How YAML structures map to JSON
| YAML | JSON |
|---|---|
mapping (key: value) | object |
sequence (- item or [a, b]) | array |
true, false | boolean |
~, null, empty value | null |
block scalars | and > | string with \n or folded spaces |
anchors &x and aliases *x | the value copied at every alias |
several documents separated by --- | an array of documents |
Comments are not part of the data, so they do not appear in the JSON.
The Sort keys option is handy when you compare two YAML files that list the same settings in a different order: convert both with sorting on and the JSON versions line up key for key. Leave it off when the order carries meaning for people reading the output, as in an OpenAPI document.
Type surprises to check before you rely on the output
This converter follows the YAML 1.2 core schema. That avoids some classic YAML 1.1 traps but not all of them:
yes,no,onandoffstay strings. A YAML 1.1 tool would read them as booleans, so if the file is shared with such tools, prefertrue/false.- Unquoted
1.10is the number 1.1, and007is 7. Quote version numbers, zip codes and IDs. - Dates such as
2024-05-01are kept as strings, because JSON has no date type. .infand.nanhave no JSON representation and are written asnull.- Merge keys (
<<: *base) are not merged; they appear as a literal"<<"property.
Common conversion errors
- Tabs in indentation – replace them with spaces.
- A colon inside an unquoted value, such as
title: Step 1: install– wrap the value in quotes. - Duplicate keys in one mapping – YAML forbids them and the converter reports the line.
If you are not sure where the problem is, the YAML validator gives the same messages while you edit.
Going the other way
To turn JSON back into YAML, use the JSON to YAML converter. Comments and the original layout cannot be recovered after a round trip, so keep the YAML as your source file. When you need the result in other shapes, the YAML converter offers XML, CSV, TOML and HTML from the same input.
Privacy
Parsing and conversion run in your browser, so your YAML is not uploaded. Load URL fetches files through our server and Save & Share stores the content for a link, only if you use them.
Convert YAML to JSON in Python
PyYAML reads the document and the standard json module writes it. Two defaults trip people up. PyYAML turns an unquoted date such as 2024-05-01 into a datetime.date, which json.dumps() rejects with TypeError: Object of type date is not JSON serializable; default=str writes it as a string instead. And json.dumps() escapes non-ASCII characters, giving caf\u00e9, unless you pass ensure_ascii=False.
# pip install pyyaml
import json
import yaml
text = """
server:
host: 0.0.0.0
port: 8080
released: 2024-05-01
motd: café
"""
data = yaml.safe_load(text)
print(json.dumps(data, indent=2, ensure_ascii=False, default=str))
For this sample the script and the converter above produce the same JSON, so the page is a quick way to see what a file should convert to before you automate it.