Skip to content
ToolkitHub

YAML to JSON

Convert YAML to JSON

Guide

YAML to JSON and the type surprises in between

By Logappradeep · ServiceNow developer

Many tools only understand JSON: jq, JSON Schema validators, REST APIs, and most programming language standard libraries. If your data lives in a Kubernetes manifest, a Compose file, or a CI pipeline written in YAML, converting it to JSON lets you query, validate, or send it anywhere. Conversion also reveals how a YAML parser actually interprets your file, which is often not what the author intended.

How YAML decides on types

Unquoted YAML values are typed by pattern: anything that looks like a number becomes a number, true and false become booleans, and null or ~ becomes null. That convenience causes real bugs. Converting this input:

version: 1.10
zip: 012345
port: 08
released: 2024-01-15
country: no

produces:

{
  "version": 1.1,
  "zip": 12345,
  "port": 8,
  "released": "2024-01-15T00:00:00.000Z",
  "country": "no"
}
  • 1.10 became 1.1 because it was read as a decimal number. Quote version numbers.
  • The ZIP code lost its leading zero. Quote postal codes, phone numbers, and IDs.
  • The date became a full timestamp because unquoted dates are parsed as date values.
  • no stayed a string here, because this parser follows the YAML 1.2 core schema. Older YAML 1.1 parsers, still common in some ecosystems, read no, yes, on, and off as booleans, which is the infamous "Norway problem" for the country code NO. Quote such values so every parser agrees.

Other YAML features and how they convert

  • Anchors and aliases (&defaults and *defaults) are expanded, so the JSON contains full copies of the shared block.
  • Comments are dropped since JSON has no place for them.
  • Block scalars using | or > become ordinary strings with \n line breaks.
  • Multiple documents separated by ---, common in Kubernetes files, are converted into a JSON array with one element per document.

Errors you might see

YAML is whitespace-sensitive. Tabs used for indentation, inconsistent indentation within a list, a missing space after a colon, and unquoted strings containing : or a leading * are the usual culprits. The error panel shows the parser's message with a line and column to guide you.

Parsing happens in your browser, so configuration details and internal hostnames stay on your machine.

Frequently asked questions

Why did my version number change from 1.10 to 1.1?

Unquoted 1.10 is a valid decimal number, and 1.10 equals 1.1. Wrap version strings in quotes so YAML keeps them as text.

Can I convert a file with multiple documents?

Yes. Documents separated by --- become a JSON array, with one element per document in the original order.

What happens to YAML comments?

They are discarded, because JSON has no comment syntax.

Why does my YAML fail to parse when it looks fine?

The most common hidden cause is a tab character used for indentation. YAML only allows spaces for indentation.