Skip to main content
Back to Blog

JSON vs YAML: Choosing the Right Config Format

When to choose YAML for config files and when to stick with JSON, and how to convert between them without losing structure.

Kashyap Thakar
8 min
DevOpsJSONYAML
JSON vs YAML: Choosing the Right Config Format

JSON (JavaScript Object Notation) and YAML (YAML Ain't Markup Language) are the two formats you will meet most often in DevOps and configuration management. They can usually represent the same data, but they make different trade-offs.

JSON: The Machine's Favorite

JSON is strict. It requires double quotes around keys, doesn't support comments, and cares little for whitespace (other than for separation).

  • Pros: Unambiguous parsing, native to JavaScript/Web, widely supported.
  • Cons: No comments allowed, verbose syntax (braces, quotes), hard to read large files.

YAML: The Human's Choice

YAML was designed for readability. It uses indentation (whitespace) to define structure, similar to Python.

  • Pros: Clean syntax, supports comments (#), advanced features like anchors and aliases.
  • Cons: Indentation errors can be subtle, unquoted values are type-guessed, and the specification is much larger than JSON's.

Comparison

JSON

{ "server": { "port": 80, "host": "localhost" } }

YAML

server: port: 80 host: localhost # comments are allowed

Advanced YAML Features

Anchors and Aliases

YAML supports referencing previously defined values:

defaults: &defaults
  timeout: 30
  retries: 3

production:
  <<: *defaults
  timeout: 60

This allows you to define common values once and reuse them, reducing duplication and errors.

Multi-line Strings

YAML handles multi-line strings elegantly:

description: |
  This is a long description
  that spans multiple lines
  and preserves line breaks.

Comments

YAML supports comments (JSON does not), making it ideal for configuration files where you need to explain settings:

# Production database settings
database:
  host: db.example.com
  port: 5432 # PostgreSQL default

Performance Comparison

JSON is generally faster to parse: its grammar is small, and JSON.parse is built into every JavaScript engine. YAML parsers implement a much larger specification (anchors, aliases, multiple scalar styles, implicit typing) and usually come from a library. For a configuration file read once at startup, that difference is negligible, so choose on readability and tooling rather than speed.

Common Pitfalls

YAML Indentation Errors

YAML uses indentation to define structure. Tabs are not allowed for indentation, and inconsistent spacing either causes a parse error or, worse, quietly moves a key to a different level:

# Wrong - inconsistent indentation
server:
  port: 80
 host: localhost # Error!

# Correct - consistent 2-space indentation
server:
  port: 80
  host: localhost

JSON Trailing Commas

JSON doesn't allow trailing commas, which can be frustrating when editing:

{
  "key": "value", // Error! Trailing comma
}

When to Use Which?

Use JSON for:

  • APIs and data interchange between services
  • When you want strict, unambiguous syntax
  • JavaScript/TypeScript projects (built-in JSON.parse)
  • Configuration that's generated programmatically

Use YAML for:

  • Configuration files (Kubernetes, Docker Compose, GitHub Actions)
  • When human readability and comments are essential
  • DevOps and infrastructure-as-code workflows
  • Files that are edited manually by developers
  • When you need advanced features like anchors and aliases

YAML Gotchas: Implicit Typing

Unquoted YAML scalars are typed by guesswork, and the rules differ between YAML versions. In YAML 1.1, yes, no, on and off are booleans, which gives the famous "Norway problem": the country code NO loads as false in a 1.1 parser such as PyYAML. YAML 1.2 narrowed booleans to true and false, and js-yaml 4 (the library behind our YAML converter) follows that. Even then, a value like 1.10 is a float, so a version number silently loses its trailing zero.

yaml.load("country: NO\nver: 1.10\nflag: yes\nempty:\nd: 2024-01-05")
// {
//   country: 'NO',
//   ver: 1.1,
//   flag: 'yes',
//   empty: null,
//   d: 2024-01-05T00:00:00.000Z
// }

The output above is from js-yaml 4.3. Notice that the unquoted date became a Date object and the empty value became null. The defensive habit is simple: quote any string that could be mistaken for another type, such as "NO", "1.10", "2024-01-05" or "on". JSON never has this problem, because strings are always quoted.

Multi-line Strings: Literal vs Folded

YAML has two block scalar styles, and confusing them is a frequent source of broken shell scripts in CI files. | keeps line breaks, while > folds lines into spaces.

yaml.load("a: |\n  l1\n  l2\nb: >\n  l1\n  l2\n")
// { a: 'l1\nl2\n', b: 'l1 l2\n' }

Use | for scripts and certificates, and > for long prose. In JSON the equivalent is one long string with \n escapes, which is exactly why embedded scripts are painful in JSON config.

What Is Lost When You Convert

JSON's data model is smaller than YAML's, so converting YAML to JSON is lossy in a few predictable ways:

  • Comments disappear, because JSON has nowhere to put them.
  • Anchors and aliases are expanded into repeated copies. Going back to YAML will not recreate them.
  • Dates become strings (or whatever your parser produces), because JSON has no date type.
  • Key order is preserved by most tools, but neither format guarantees it as meaningful.
yaml.load("a: &d\n  t: 30\nb:\n  <<: *d\n  t: 60")
// { a: { t: 30 }, b: { t: 60 } }

The merge key (<<) is widely supported but is a YAML 1.1 convention rather than part of the YAML 1.2 core schema, so check that your tool understands it before relying on it. Try the round trip on your own file with the YAML to JSON converter and compare what changed.

Getting Comments Into JSON

Standard JSON (RFC 8259) defines no comment syntax, so a stray // is a syntax error. Two workarounds are common. Some tools accept supersets such as JSONC (JSON with comments) or JSON5, but only where the tool documents it, and a generic JSON.parse will reject them. The portable alternative is a documentation key, such as "_comment", which the consuming program ignores. It is clumsy, but it survives every parser.

Most strict JSON errors are small. Here is what JSON.parse says for the two classic mistakes in Node 24:

JSON.parse('{"a":1,}')
// Expected double-quoted property name in JSON at position 7 (line 1 column 8)

JSON.parse("{'a':1}")
// Expected property name or '}' in JSON at position 1 (line 1 column 2)

Error wording differs between engines and versions, but the position is always your best clue. Paste the document into our JSON editor to get the line and column of the first error, and read why a dedicated JSON editor pays off for more tips.

Debugging YAML Indentation

  • Never use tabs. The YAML specification forbids tab characters for indentation. Configure your editor to insert spaces for .yml and .yaml files.
  • Read the line number. Parsers report where they gave up, not where you made the mistake. Check the line above it first.
  • Convert to JSON to check structure. If the converted JSON nests differently than you intended, the indentation is wrong even though the YAML parsed.
  • Lint in CI. A YAML linter or schema check catches errors before a deploy does.
yaml.load("server:\n  port: 80\n host: localhost")
// bad indentation of a mapping entry (3:2)

Schemas and Safe Loading

Both formats are only as reliable as their validation. JSON Schema is the most widely used way to validate structure, and because YAML parses to the same data model, the same schema can validate a YAML config once it is loaded. Editors use this to give autocomplete for files like Kubernetes manifests.

On the security side, treat YAML from untrusted sources carefully. Some YAML libraries can construct arbitrary language objects from tags if you use their unsafe loader. Use the safe or default-restricted loader, and validate the result against a schema. JSON parsers only produce plain data, which is one reason JSON is the safer choice for anything crossing a trust boundary. Keep secrets out of both formats and in environment variables, as covered in environment variable best practices.

Convert Between Them

Migrating a project? Use our JSON to YAML or YAML to JSON converters. Invalid input is reported as an error rather than silently converted. They are handy for:

  • Migrating configuration files between formats
  • Checking how a YAML file will look as JSON, and vice versa

All conversion happens client-side, ensuring your configuration data remains private and secure.

Frequently Asked Questions

Is YAML better than JSON for configuration?

For files humans edit by hand, usually yes: comments, less punctuation and multi-line strings make it easier to maintain. For files generated or consumed by programs, JSON is usually better because parsing is unambiguous and tooling is universal.

Can I convert JSON to YAML without losing data?

Yes, in terms of data. YAML 1.2 was designed so that valid JSON is also valid YAML, so every JSON value has a YAML equivalent. The converter uses js-yaml, whose output quotes strings such as 'NO' or '2024-01-05' so they stay strings. Use the JSON to YAML converter, and still skim the result for values a different YAML parser might read as another type.

Why does my YAML value turn into a boolean or number?

Unquoted scalars are resolved by type rules, so yes, on, 1.10 or a date-like string can be read as something other than text, depending on the YAML version. Wrap the value in quotes to force a string.

Does YAML allow tabs?

Not for indentation. The specification requires spaces for indenting block structure, so tabs there cause parse errors. Tabs inside a quoted string are fine.

Written by

Spotted a mistake? Let me know