Skip to content

YAML to JSON Converter and Validator (2027 Guide)

Convert YAML to JSON or JSON to YAML in your browser, with the line and column of any error.

JSON

Converted in your browser with js-yaml (YAML 1.2). Comments are dropped: JSON has none.

By · Updated

How do you use the YAML to JSON converter?

Flow chart of the converter: paste YAML or JSON text or drop a file, choose the direction, choose an indent of 2 or 4 spaces, copy or download the converted text, and if the input is not valid, read the error with its line and column.
Using the YAML to JSON converter, step by step

Paste YAML or JSON text into this tool, or drop a file onto it, then choose a direction and an indent. The result appears as text, ready to copy or download. Follow these steps:

  1. Paste YAML or JSON text, or drop a file onto the tool.
  2. Choose the direction: YAML to JSON, or JSON to YAML.
  3. Choose an indent of 2 or 4 spaces.
  4. Use copy to take the converted text, or download to save it as a file.
  5. If the input is not valid, read the error message. It shows the line and column where the problem is.

Check the direction before you copy the result, since the output is written in the other format. The output tells you what the tool checked. Valid input gives converted text. Invalid input gives an error with its line and column, so this tool works as a quick YAML validator and formatter.

The check covers syntax only. The tool has no schema input, so it cannot test whether your data has the fields you expect. The last section of this guide covers schema checks.

A good way to learn the tool is to start with a small file, such as one key and one value. Add lines a few at a time, and convert after each change. When a conversion fails, the line and column in the error show where the new text went wrong.

How does the YAML to JSON converter work?

This tool parses YAML with js-yaml, using the YAML 1.2 core schema, and parses JSON with the browser's own JSON parser. It then writes the other format. The js-yaml usage notes document the library's functions.

A multi-document YAML file becomes a JSON array. Comments are dropped, since JSON has none. Nothing leaves the browser, so the text you paste is processed in your own browser.

Here is a small example. The YAML below has a comment, a mapping and a list.

YAML
# Example config
server:
  host: example.test
  ports: [80, 443]

After conversion, the comment is gone. The mapping becomes an object, and the list becomes an array. The result looks like this:

JSON
{
  "server": {
    "host": "example.test",
    "ports": [
      80,
      443
    ]
  }
}

Walk through the change from the top. The first line is a comment, so it has no JSON counterpart and disappears. The key server sits at the top level, and the lines indented beneath it become an object inside the outer object. The host line becomes a key with a string value, because example.test is plain text rather than a number, boolean or null. The square brackets hold a list, which becomes a JSON array with the same values in the same order.

Indent matters in the YAML and not in the JSON. In YAML, indentation sets the nesting, so the spaces must be right. In the JSON output, indentation is only layout. Choosing 2 or 4 spaces in the tool changes how the result looks, not what it holds.

What is the difference between YAML and JSON, and when should I use each?

YAML and JSON store the same kinds of data, but their syntax differs. The table compares them on the points that matter most when you convert between the two.

FeatureYAMLJSON
StructureIndentation sets nestingObjects and arrays
CommentsBegin with #Not supported
Literal namesIn the YAML 1.2 core schema, only true and false forms are booleanstrue, false and null must be lowercase
Typical useConfig that people edit by handData that programs pass to each other

JSON represents four primitive types (strings, numbers, booleans and null) and two structured types (objects and arrays). Its literal names must be lowercase. Text exchanged between systems outside a closed ecosystem must be encoded as UTF-8. These rules come from the JSON standard.

The current YAML specification revision is 1.2.2, dated October 1, 2021, as set out in the YAML 1.2.2 specification. Its primary focus was making YAML a strict superset of JSON. In other words, a valid JSON document is also valid YAML, and the FAQ at the end of this guide returns to that point.

The same config in both formats

Here is one settings file in YAML, with a comment, then the same data in JSON. Both are shown in full so you can compare them line by line.

YAML
# Deployment settings, edited by hand
app:
  name: demo
  debug: false
  ports:
    - 80
    - 443
  owner: null
JSON
{
  "app": {
    "name": "demo",
    "debug": false,
    "ports": [
      80,
      443
    ],
    "owner": null
  }
}

Read the two side by side. The YAML needs no quotes around demo, and it has no braces or commas. Its list uses a dash at the start of each item, while the JSON list sits between square brackets with commas. The comment at the top has no place in the JSON, so conversion drops it. Both versions hold the same data, and the value null means the same thing in each.

Use YAML when a person edits the file and comments help. Use JSON when a program reads or writes the data and nobody needs to annotate it. A common pattern is to keep the YAML as the source you edit, and to convert to JSON only when a tool needs it.

How does YAML map to JSON?

Most conversions follow a short set of rules. Mappings become objects, and sequences become arrays. Scalars become strings, numbers, booleans or null. The table sums up how each part of a YAML file comes out.

YAML partJSON result
MappingObject
SequenceArray
Scalar (string, number, boolean or null)String, number, boolean or null, as the schema decides
CommentDropped, since JSON has none
Several documentsOne JSON array
Anchor and aliasCheck the JSON output for the repeated data

Scalars depend on the schema. Under the YAML 1.2 core schema, only true, True, TRUE, false, False and FALSE resolve to booleans, and null, Null, NULL and ~ resolve to null. Under the YAML 1.2 JSON schema, only true and false resolve to booleans. Words that match none of these rules are read as strings, so the schema you use decides what a value becomes.

YAML 1.2 writes octal numbers with a 0o prefix, as in 0o14. Check any value that looks like an octal number before you convert it.

A YAML stream may contain several documents, and three dashes (---) mark the start of a document. This tool writes all of them as one JSON array. In code, js-yaml's loadAll returns an array for a multi-document stream. With yq, the eval-all command reads all documents of all given files into memory, then runs your expression once against them.

YAML can also reuse a node. The YAML 1.2.2 specification says a repeated node is marked with an anchor using the ampersand (&) and referenced later with an alias using the asterisk (*). After you convert a file that uses anchors and aliases, read the JSON output and confirm that the repeated data appears the way you expect.

Comments are lost in conversion. YAML comments begin with the # character, and JSON has no comments to hold them. Keep the YAML file as your source if the comments matter, because the JSON cannot bring them back.

How do you convert YAML to JSON in Python, JavaScript and on the command line?

Convert in Python with PyYAML, in JavaScript with js-yaml, or on the command line with yq. Each can also convert JSON back to YAML. The examples use sample.yml as a file name, so replace it with your own file.

In Python

The PyYAML documentation warns that yaml.load is not safe to call on data from an untrusted source. Use yaml.safe_load instead. It recognizes only standard YAML tags and cannot construct an arbitrary Python object. Then convert the result with json.dumps, which pretty-prints JSON with the indent you pass. By default it escapes non-ASCII characters, as the Python json documentation explains.

Python
import json
import yaml

yaml_text = 'name: demo'
data = yaml.safe_load(yaml_text)
json_text = json.dumps(data, indent=2)

Step through the code. yaml.safe_load reads the YAML text and returns Python data. json.dumps turns that data into a JSON string, and indent=2 sets the indent of the result. Non-ASCII characters appear as escape sequences, because ensure_ascii defaults to True.

For a file with several documents, use yaml.safe_load_all, which is the safe counterpart for a stream of documents. Collect the documents in a list before you call json.dumps.

Python
import json
import yaml

documents = list(yaml.safe_load_all(yaml_text))
json_text = json.dumps(documents, indent=2)

Going back to YAML, json.loads parses a JSON string and raises JSONDecodeError on invalid input. yaml.safe_dump then writes the data as YAML, using only standard tags.

Python
import json
import yaml

data = json.loads('{"name": "demo"}')
yaml_text = yaml.safe_dump(data)

When the JSON comes from a user, wrap json.loads in a try block and catch JSONDecodeError, so a bad input gives a clear message instead of a crash.

In JavaScript

The js-yaml usage notes list load() and dump(). Use loadAll() for a multi-document stream, because it accepts empty and multi-document streams and returns an array, unlike load(). Then write the JSON with JSON.stringify. Its third argument sets the indentation in spaces, clamped to 10, as MDN explains.

JavaScript
const data = yaml.load(yamlText);
const jsonText = JSON.stringify(data, null, 2);

For JSON to YAML, JSON.parse reads the text. It throws a SyntaxError on text that is not valid JSON, so catch that error when the input comes from a user. The yaml.dump call then writes the YAML.

JavaScript
const data = JSON.parse(jsonText);
const yamlText = yaml.dump(data);

On the command line

The yq conversion guide by mikefarah uses -o=json to write JSON from YAML, and -p=json to read JSON input. The --indent flag, or -I, sets the indent. Its default is 2, also for JSON output.

Shell
yq -o=json '.' sample.yml
yq -o=json --indent 4 '.' sample.yml
yq -p=json sample.json

The last line converts JSON to YAML. The jq manual describes a different job. jq runs filters on a stream of JSON data, and its identity filter, a single dot, outputs its input unchanged. The --indent option sets the indent, with no more than 7 spaces.

Shell
jq --indent 4 '.' sample.json

Use jq after conversion to pretty-print JSON you already have.

How do you convert JSON back to YAML?

Converting back is not a perfect round trip, because JSON has no comments to carry over. Follow these steps when you move data from JSON to YAML, whether you use the tool or yq.

  1. Check the JSON first. The literal names true, false and null must be lowercase, and the text must be UTF-8.
  2. Choose JSON to YAML in the tool, or run the yq command with -p=json.
  3. Choose the indent. The tool offers 2 or 4 spaces, and yq defaults to 2.
  4. Compare the YAML with your JSON, key by key, and check that each value kept its type.
  5. Quote any string that looks like a boolean or null, such as "true" or "null", so YAML keeps it as text under the core schema.
  6. Add comments by hand. JSON had none, so the converted YAML has none either.

Here is the earlier example, written back into YAML by hand with a comment added:

YAML
# Example config
server:
  host: example.test
  ports: [80, 443]

The comment is the part a conversion cannot restore. Keep the file in YAML from here on, and save a JSON copy only when a program needs one.

Why does my YAML fail to parse?

Most parse failures come from tabs, indentation or unquoted values. Fix each one before you convert again. The sections below cover tabs and indentation first, then the Norway problem, and the checklist at the end ties them together.

In YAML, tab characters must not be used for indentation, since different systems treat tabs differently, as the YAML 1.2.2 specification states. Replace tabs with spaces at the start of each line. A file that mixes tabs and spaces is a common cause of confusing errors.

Indentation sets structure, so one misaligned line can break the whole file. The Helm documentation shows the error 'error converting YAML to JSON: yaml: line 7: did not find expected key' for a badly indented multi-line string. Start at the line number in the message, and compare its indentation with the lines above it. When this tool reports an error, it gives the line and column, so fix that spot first and convert again.

What is the Norway problem in YAML?

Grid comparing unquoted values: under the YAML 1.1 boolean type, NO becomes false and yes and on become true; under the YAML 1.2 Core Schema, NO, yes and on are not booleans and stay text.
Which unquoted values become booleans

The Norway problem comes from YAML 1.1. Its boolean type, from a working draft dated January 18, 2005, treats y, yes, no, n, on and off as booleans, in several capitalizations, including NO. An unquoted NO can therefore become false.

Unquoted valueYAML 1.1 boolean typeYAML 1.2 Core Schema
NOBoolean, falseNot a boolean (text)
yesBoolean, trueNot a boolean (text)
onBoolean, trueNot a boolean (text)

The YAML 1.2 Core Schema resolves only true, True, TRUE, false, False and FALSE to booleans, so NO stays text under it. This tool reads YAML with the 1.2 core schema, so an unquoted NO stays text here. A YAML 1.1 parser may read it as false, so check which schema your own parser uses.

Quote any value that a parser could read two ways. Quote country codes and similar short words when you are unsure which schema a parser uses. Octal numbers need the same care, because YAML 1.2 writes them with a 0o prefix, as in 0o14.

What should I check when a conversion fails?

Work through this checklist in order. Each step is small, and most failures are fixed by one of them.

  1. Read the line and column. This tool reports both in its error message, and Helm's error names a line number. Start there, not at the top of the file.

  2. Find any tab characters. YAML does not allow tabs for indentation, so replace each one with spaces.

  3. Compare the indent of the flagged line with the lines above it. A nested key must sit under its parent, and one misaligned line can break the whole file.

  4. Check multi-line strings. Helm's docs show a badly indented multi-line string behind the error 'did not find expected key'.

  5. Look for quoted numbers. A quoted value is treated as a string, so a quoted port fails if Kubernetes expects an integer. Remove the quotes around the port.

  6. Look for unquoted special words. Under YAML 1.1, NO, yes, on and off can become booleans. Quote them if your parser uses that schema.

  7. For JSON input, check the literal names and the encoding. true, false and null must be lowercase, and the text must be UTF-8.

  8. Cut the file down. Convert the failing part alone, then add back one section at a time until the error returns.

How is YAML used in Kubernetes, GitHub Actions and OpenAPI?

YAML appears in three common places, and each has its own rule. The table summarizes them.

WhereFormat ruleDetail
KubernetesManifests are YAML by convention, and JSON also workskubectl converts a manifest to JSON or another supported format when it makes the API request
GitHub ActionsWorkflow files use YAML syntaxThey must end in .yml or .yaml, and live in .github/workflows
OpenAPI 3.1A document may be JSON or YAMLYAML 1.2 is recommended, and tags are limited to the JSON Schema ruleset

Kubernetes manifests are YAML by convention, although JSON also works. Tools such as kubectl convert a manifest into JSON or another supported format when they make the API request, so a YAML manifest does not have to be JSON on disk. The Kubernetes documentation explains this.

GitHub Actions workflow files use YAML syntax. They must have a .yml or .yaml extension, and they are stored in the .github/workflows directory, according to the GitHub workflow syntax reference. A syntax slip in one of these files is a good reason to run it through a YAML checker first.

An OpenAPI 3.1 document may be written in JSON or YAML. To keep round-tripping between the two possible, the specification recommends YAML 1.2, and its tags must be limited to the JSON Schema ruleset.

Converting any of these files with this tool gives you the same data in JSON, without the comments. Keep the YAML file as the source you edit.

How do you validate YAML against a schema?

Flow chart of schema validation in three steps: parse the YAML (a syntax error stops you here), convert the parsed data to JSON, then validate the JSON against your JSON Schema with a JSON Schema validator.
Validating YAML against a JSON Schema

Parsing only checks syntax, so a schema check is a separate step. You convert the YAML to JSON, then validate the result against a JSON Schema. JSON Schema is a declarative language for defining the structure and constraints of JSON data. Its latest version is 2020-12, which OpenAPI 3.1 also builds on.

Helm is a concrete example. A Helm chart can define a JSON Schema for its values in values.schema.json. Validation occurs when you run helm install, helm upgrade, helm lint or helm template. You can skip it with --skip-schema-validation, as the Helm chart documentation explains.

A simple workflow for your own files has three steps.

  1. Parse the YAML. A syntax error stops you here, and the line and column from this tool help you find it.
  2. Convert the parsed data to JSON, as shown earlier in this guide.
  3. Validate the JSON against your JSON Schema with a JSON Schema validator. Each error points to a rule that the data broke.

This tool does not run a schema check. Its errors cover syntax, so use a JSON Schema validator for structure, after conversion.

More guides in Developer Tools.

Frequently asked questions

How do I convert YAML to JSON in Python?
Read the YAML with PyYAML's yaml.safe_load, then write it with json.dumps and an indent such as 2. Avoid yaml.load on files you did not write, because PyYAML's documentation says it is not safe for untrusted data.
Should I use YAML or JSON for config files?
YAML suits config that people edit by hand, because it allows comments. JSON suits data that programs pass to each other. Kubernetes, GitHub Actions and OpenAPI all accept YAML, and OpenAPI also accepts JSON.
Is JSON valid YAML?
Yes. YAML 1.2 was designed to make YAML a strict superset of JSON. Python's json documentation calls JSON a subset of YAML 1.2, and Helm's docs say any valid JSON document should be valid YAML.
What does 'error converting YAML to JSON' mean in Helm?
It means Helm could not read a YAML file. Helm's docs show this error for a badly indented multi-line string, with a line number, so check the indentation at that line. Quoted values are treated as strings, so a quoted port fails if Kubernetes expects an integer.
What is the Norway problem in YAML?
It is a YAML 1.1 behavior where unquoted values such as NO become booleans. The YAML 1.2 Core Schema reads NO as text, so quote such values if your parser uses YAML 1.1.

Sources

  1. The js-yaml library for JavaScript supports YAML 1.2 and 1.1 and exports load() and dump() (github.com)
  2. JSON represents four primitive types (strings, numbers, booleans and null) and two structu (www.rfc-editor.org)
  3. YAML 1.2 was designed to make YAML a strict superset of JSON. (yaml.org)
  4. yq's eval-all (alias ea) reads all documents of all given YAML files into memory and runs (mikefarah.gitbook.io)
  5. PyYAML's yaml.safe_load(stream) and yaml.safe_load_all(stream) recognize only standard YAM (pyyaml.org)
  6. Python's json documentation states that JSON is a subset of YAML 1.2. (docs.python.org)
  7. In JavaScript, JSON.stringify(value, replacer, space) uses a numeric space argument as the (developer.mozilla.org)
  8. With mikefarah's yq, `yq -o=json '.' sample.yml` converts YAML to JSON, `yq -p=json sample (mikefarah.gitbook.io)
  9. jq runs filters on a stream of JSON data; the identity filter . outputs its input unchange (jqlang.org)
  10. Helm's docs say that because YAML is a superset of JSON, any valid JSON document should be (helm.sh)
  11. The YAML 1.1 boolean type (working draft of January 18, 2005) treats y, yes, no, n, on and (yaml.org)
  12. Kubernetes manifests are YAML by convention (JSON also works), and tools such as kubectl c (kubernetes.io)
  13. GitHub Actions workflow files use YAML syntax, must have a .yml or .yaml extension and are (docs.github.com)
  14. An OpenAPI 3.1 document may be written in JSON or YAML; to keep round-tripping between the (spec.openapis.org)
  15. JSON Schema is a declarative language for defining the structure and constraints of JSON d (json-schema.org)
  16. A Helm chart can define a JSON Schema for its values in values.schema.json; validation run (helm.sh)

All free tools