YAML to JSON
YAML · YML · JSON · Validator
Paste YAML in the first box.

How YAML becomes JSON

YAML

# comments are dropped
name: GeekTools
version: "1.10"
port: 8080
debug: false
tags: [web, api]
base: &base
  retries: 3
job:
  <<: *base
  cmd: |
    echo one
    echo two

JSON

{
  "name": "GeekTools",
  "version": "1.10",
  "port": 8080,
  "debug": false,
  "tags": ["web", "api"],
  "base": { "retries": 3 },
  "job": {
    "retries": 3,
    "cmd": "echo one\necho two\n"
  }
}
  • Mappings (key: value) become JSON objects, and lists (- item or [a, b]) become arrays.
  • Plain values get a type: 8080 is a number, true/false are booleans, null, ~ or nothing at all is null. Everything else is text. Put a value in quotes ("1.10") to keep it as text.
  • Anchors and aliases (&base, *base) are copied in full, and the merge key <<: *base copies the keys of base into the mapping; keys written in the mapping itself win.
  • Block text: | keeps the line breaks, > joins the lines with spaces. Both end with one line break; |- and >- remove it.
  • Dates such as 2024-01-05 stay text. JSON has no date type, and turning them into a time would add a time zone that was not in your file.
  • Several documents separated by --- become a JSON array with one item per document.
  • .inf and .nan have no JSON form and become null (the status line warns you). Whole numbers longer than 15 digits are kept exactly.

Common YAML errors and how to fix them

ErrorWrongRight
Tab used for indentationapp:
→port: 80
Use spaces. The Replace tabs button in the error message does it for you.
Missing space after the colonport:80port: 80 (without the space, port:80 is one piece of text)
Items not lined upa: 1
 b: 2
Keys of the same mapping start in the same column.
The same key twicename: a
name: b
Each key once per mapping. Many parsers silently keep the last one; this page shows an error.
A colon or # inside texttitle: Note: read metitle: "Note: read me"
Text starting with a special characterpath: *.log or tag: @adminpath: "*.log", tag: "@admin"
Version numbers lose a zeropython: 3.10 (the number 3.1)python: "3.10"

YAML or JSON: which one to use?

Both describe the same kinds of data: objects, lists, text, numbers, true/false and null. Since YAML 1.2, every JSON file is also valid YAML, so a YAML parser can read JSON as it is.

YAMLJSON
CommentsYes, with #No
StructureBy indentation (spaces only)By { } and [ ]; spaces do not matter
Quotes around textUsually not neededAlways, double quotes
Reuse a blockAnchors, aliases, merge keysNo
Multi-line text| and > blocksOne line with \n
Typical useConfig files people edit: Kubernetes, Docker Compose, GitHub Actions, Ansible, CloudFormation, OpenAPIAPIs, data exchange, package.json; everything a program writes
ParsingComplex; small mistakes change the meaningSimple and strict; fast everywhere

A common pattern: people write and review YAML, and a build step converts it to JSON for the program or API that needs it.

Convert YAML and JSON in code and on the command line

ToolYAML to JSONJSON to YAML
yq (Go)yq -o=json '.' config.yamlyq -P '.' data.json
Python (PyYAML)json.dumps(yaml.safe_load(open('c.yaml')), indent=2)yaml.safe_dump(json.load(open('d.json')), sort_keys=False)
Node.js (js-yaml)JSON.stringify(yaml.load(text), null, 2)yaml.dump(JSON.parse(text))
Rubyruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.load_file("c.yaml"))'ruby -ryaml -rjson -e 'puts JSON.parse(File.read("d.json")).to_yaml'
PowerShell 7Install-Module powershell-yaml, then Get-Content c.yaml -Raw | ConvertFrom-Yaml | ConvertTo-Json -Depth 20Get-Content d.json -Raw | ConvertFrom-Json | ConvertTo-Yaml
kubectlkubectl get deploy web -o jsonkubectl get deploy web -o yaml
AWS CLI (cfn-flip)cfn-flip template.yaml template.jsoncfn-flip template.json template.yaml

Use a safe loader (yaml.safe_load, not yaml.load) for YAML from other people: some loaders can create program objects from special tags. This page never runs anything from the file.

Frequently asked questions

How do I convert YAML to JSON?

Choose "YAML to JSON", then paste the YAML or click "Open file" to open a .yaml or .yml file. The JSON appears at once in the second box. Choose the indent, then copy the JSON or download it as a .json file. If the YAML has an error, the status line shows the line and column, and "Show me" selects that spot in the box.

How do I convert JSON to YAML?

Choose "JSON to YAML" and paste the JSON. Every object becomes a block of key: value lines, and every array becomes a list of "- " items. Text that YAML would read as something else, such as "yes", "1.10" or "2024-01-05", is put in quotes, so the YAML means exactly the same as your JSON.

Why are the comments from my YAML missing in the JSON?

JSON has no comments, so they cannot be kept. The same is true for anchors (&name) and aliases (*name): JSON has no way to point to another value, so every alias is replaced by a full copy of the data. If you convert the JSON back to YAML, the comments and anchors do not come back.

Why does "no" stay text and not become false?

YAML 1.2, the current version, only treats true and false as booleans. The older YAML 1.1 also read yes, no, on and off as booleans, which famously turned the country code NO (Norway) into false. This page follows YAML 1.2 by default, like most modern parsers. If your file was written for YAML 1.1, for example an older Ansible playbook, turn on "yes/no/on/off are booleans".

Can I convert a Kubernetes file with several documents (---)?

Yes. When a YAML file has more than one document, separated by lines with ---, the JSON becomes an array with one item per document. In the other direction, turn on "Array items as separate documents" and a JSON array becomes one YAML document per item, separated by ---, which is the format kubectl apply -f expects.

Does it work with AWS CloudFormation templates (!Ref, !Sub)?

Yes. CloudFormation short tags are turned into the long JSON form: !Ref MyBucket becomes {"Ref": "MyBucket"}, !Sub becomes {"Fn::Sub": ...} and !GetAtt Role.Arn becomes {"Fn::GetAtt": ["Role", "Arn"]}. That is the same JSON that CloudFormation itself accepts. Other custom tags are removed and their value is kept, and the status line tells you which tags it found.

Is my YAML or JSON uploaded?

No. The conversion runs with JavaScript in your browser (the open-source js-yaml parser, MIT license), and nothing is sent to a server. You can safely paste configuration files that contain passwords or keys. Only your settings, such as the indent, are kept in this browser for your next visit.

More developer tools