CleanTextly

How to Convert YAML to JSON

A practical guide to moving data between YAML and JSON — with a worked example, the manual process, and the mistakes that most often trip up a conversion.

YAML and JSON are two of the most widely used formats for structured data. YAML tends to show up in hand-edited configuration files, while JSON dominates APIs and data exchange between programs. Because both describe the same kinds of data — objects, lists, strings, numbers, booleans, and null — it's common to need to move a file from one to the other.

This guide explains what each format is, walks through a small YAML to JSON example, covers the manual process and its pitfalls, and shows how to convert YAML to JSON online when you'd rather not do it by hand.

What Is YAML?

YAML (short for "YAML Ain't Markup Language") is a data format designed to be easy for people to read and write. Instead of braces and brackets, it uses indentation to show structure, and strings usually don't need quotes. It also supports comments, which is a big reason it's popular for configuration. You'll commonly see YAML in Docker Compose files, Kubernetes manifests, GitHub Actions workflows, and OpenAPI specifications.

What Is JSON?

JSON (JavaScript Object Notation) is a lightweight text format for structured data. It uses curly braces for objects, square brackets for arrays, and double quotes around every key and string. The grammar is small and strict, which makes JSON easy for programs to parse and generate — nearly every language ships with a JSON parser, and most web APIs send and receive it.

Compared with YAML, the trade-offs are that JSON has no comments and takes more punctuation to write by hand. The data model, though, is essentially the same, which is what makes conversion between the two straightforward.

Why Convert YAML to JSON?

  • Working With APIs

    Many APIs accept and return JSON, so YAML configuration or data often has to be converted before it can go into a request body.

  • Configuration Migration

    Moving settings between tools or platforms that expect different formats, such as a YAML config into a JSON-based one.

  • Tools That Require JSON

    Some validators, libraries, and scripts only read JSON, even when the source data was written in YAML.

  • Inspecting & Processing Data

    JSON is easy to load in code and to query with common tooling, and a converted copy can make deeply nested YAML easier to inspect.

YAML vs JSON Example

Here is a small YAML document and the equivalent JSON:

YAML

name: CleanTextly
version: 1
features:
  - JSON Formatter
  - YAML Converter
active: true

JSON

{
  "name": "CleanTextly",
  "version": 1,
  "features": [
    "JSON Formatter",
    "YAML Converter"
  ],
  "active": true
}

Each YAML key becomes a quoted JSON key, the dash-prefixed list becomes an array, and the values keep their types: 1 stays a number and true stays a boolean, while the text values become quoted strings. The two documents describe exactly the same data.

How to Convert YAML to JSON Manually

Converting by hand is mostly a matter of translating syntax, since the underlying data is the same.

  1. 1

    Read the structure from the indentation

    Lines at the same indentation level belong to the same object, and a more-indented block belongs to the key above it.

  2. 2

    Wrap objects in braces

    Turn each key: value pair into a double-quoted "key": value pair, separated by commas and wrapped in curly braces.

  3. 3

    Turn dash lists into arrays

    Each - item line becomes an element of a square-bracket array, separated by commas.

  4. 4

    Keep the value types

    Numbers, true, false, and null stay unquoted, while text values get double quotes.

  5. 5

    Check the result with a JSON parser

    A missing comma or stray quote is easy to introduce by hand, so run the result through a JSON validator before using it.

That works for a handful of lines. For anything longer or deeply nested, hand-editing is slow and error-prone — a single missing comma breaks the whole document — so it's better to let a parser do the work. Note also that comments are lost in the process: JSON has nowhere to put them.

How to Convert YAML to JSON Online

A browser-based converter does the same translation using a real YAML parser: it reads your YAML, builds the data structure, and prints it back out as formatted JSON. CleanTextly's YAML to JSON Converter works this way — paste your YAML and the JSON appears with 2-space indentation, ready to copy. If the YAML can't be parsed, it shows an error message, usually with the line and column, instead of producing partial output. The conversion runs in your browser, so your data isn't uploaded.

If you need to reformat or minify the resulting JSON afterwards, the JSON Formatter handles that.

Common YAML to JSON Conversion Problems

  • Tabs for Indentation

    YAML doesn't allow tab characters for indentation. Use spaces, or the parser will report an error.

  • Inconsistent Nesting

    Sibling keys must start at the same column. A line indented differently from its siblings is reported as a mismatch.

  • Malformed Lists

    List items should line up under their key. A misaligned dash may not raise an error at all, as shown below, so it's worth scanning the JSON output.

  • Missing Space After a Colon

    name: value is a key and a value, but name:value with no space is read as one plain string.

  • Unexpected Types

    Unquoted values are interpreted: 1.0 becomes the number 1, and true becomes a boolean. Quote a value, such as "1.0", to keep it as a string.

  • Duplicate Keys

    Repeating a key inside the same mapping is an error in most parsers, since a JSON object can't hold two values for one key.

A Misaligned List Item Doesn't Always Cause an Error

YAML

features:
  - JSON Formatter
   - YAML Converter

JSON

{
  "features": [
    "JSON Formatter - YAML Converter"
  ]
}

The second line is read as a continuation of the first item's text, so the parser is satisfied but the data is wrong. Skimming the JSON output is a quick way to catch this kind of mistake.

Can You Convert JSON Back to YAML?

Yes. Because both formats describe the same kinds of data, converting in the other direction is just as mechanical: objects become indented key-value pairs, arrays become dash lists, and quotes are dropped where they aren't needed. Structure, key order, and value types carry over.

The one thing that can't carry over is comments. JSON has none, so if you convert YAML to JSON and back again, any comments in the original are gone. CleanTextly's converter works in both directions — use the direction toggle to switch between YAML → JSON and JSON → YAML on the same YAML converter page.

YAML vs JSON: Which Should You Use?

AspectYAMLJSON
StructureIndentation and dashesBraces, brackets, and commas
CommentsSupported with #Not supported
QuotingStrings are usually unquotedKeys and strings must be double-quoted
Typical useHand-edited configuration filesAPIs and data exchange between programs
Main pitfallIndentation and type-guessing mistakesVerbose to write; a stray comma breaks parsing

Neither format is better everywhere. YAML's readability and comment support suit files that people edit by hand, while JSON's strict, simple grammar suits data exchanged between programs. In practice the choice usually follows the ecosystem you're working in, and converting between the two is a routine task.

Conclusion

YAML and JSON represent the same kinds of data with different syntax, so moving between them is mostly a matter of translating structure and keeping value types intact. Short snippets can be converted by hand, but for anything larger a parser is faster and safer — and it's worth checking the output for type surprises like 1.0 becoming 1.

When you're ready, paste your YAML into the YAML to JSON Converter to get formatted JSON in seconds, or switch direction to go from JSON to YAML.

Ready to Convert Your YAML?

Paste your YAML into CleanTextly's free converter and get formatted JSON instantly.