YAML to JSON Converter (and JSON to YAML)
Paste YAML or JSON, or open a file. The result updates as you type, and any YAML error is shown with its line and column. Anchors, merge keys, several documents and CloudFormation tags work. Everything runs in your browser.
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 (- itemor[a, b]) become arrays. - Plain values get a type:
8080is a number,true/falseare booleans,null,~or nothing at all isnull. 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<<: *basecopies the keys ofbaseinto 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-05stay 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. .infand.nanhave no JSON form and becomenull(the status line warns you). Whole numbers longer than 15 digits are kept exactly.
Common YAML errors and how to fix them
| Error | Wrong | Right |
|---|---|---|
| Tab used for indentation | app:→port: 80 | Use spaces. The Replace tabs button in the error message does it for you. |
| Missing space after the colon | port:80 | port: 80 (without the space, port:80 is one piece of text) |
| Items not lined up | a: 1 b: 2 | Keys of the same mapping start in the same column. |
| The same key twice | name: aname: b | Each key once per mapping. Many parsers silently keep the last one; this page shows an error. |
| A colon or # inside text | title: Note: read me | title: "Note: read me" |
| Text starting with a special character | path: *.log or tag: @admin | path: "*.log", tag: "@admin" |
| Version numbers lose a zero | python: 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.
| YAML | JSON | |
|---|---|---|
| Comments | Yes, with # | No |
| Structure | By indentation (spaces only) | By { } and [ ]; spaces do not matter |
| Quotes around text | Usually not needed | Always, double quotes |
| Reuse a block | Anchors, aliases, merge keys | No |
| Multi-line text | | and > blocks | One line with \n |
| Typical use | Config files people edit: Kubernetes, Docker Compose, GitHub Actions, Ansible, CloudFormation, OpenAPI | APIs, data exchange, package.json; everything a program writes |
| Parsing | Complex; small mistakes change the meaning | Simple 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
| Tool | YAML to JSON | JSON to YAML |
|---|---|---|
| yq (Go) | yq -o=json '.' config.yaml | yq -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)) |
| Ruby | ruby -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 7 | Install-Module powershell-yaml, then Get-Content c.yaml -Raw | ConvertFrom-Yaml | ConvertTo-Json -Depth 20 | Get-Content d.json -Raw | ConvertFrom-Json | ConvertTo-Yaml |
| kubectl | kubectl get deploy web -o json | kubectl get deploy web -o yaml |
| AWS CLI (cfn-flip) | cfn-flip template.yaml template.json | cfn-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.