YAML to JSON
Convert YAML documents to JSON format. Mappings become objects, sequences become arrays, and YAML date values are serialised as ISO 8601 strings.
Why Convert YAML to JSON?
YAML and JSON share the same underlying data model - both can represent mappings (key-value pairs), sequences (ordered lists), and scalar values (strings, numbers, booleans, and null). The difference is audience: YAML is designed for human authoring (it supports comments, multi-line strings, and a less syntactically strict format), while JSON is designed for machine consumption (its unambiguous syntax is universally supported by APIs, databases, and data pipelines with zero configuration).
Converting YAML to JSON lets you author configuration in the more ergonomic YAML format
and produce JSON for tools that require it - REST APIs, jq
queries, MongoDB imports, and Terraform variable files all consume JSON natively.
Conversion Rules
- Mappings become JSON objects (
). Keys are always serialised as strings. - Sequences become JSON arrays (
[]). Item order is preserved. - Strings, integers, floats, and booleans map directly to their JSON equivalents.
- Null (
null,~, or empty value) becomes JSONnull. - Date values (e.g.
2024-01-15) are parsed by js-yaml into JavaScript Date objects and then serialised as ISO 8601 strings (e.g."2024-01-15T00:00:00.000Z"). - Comments are stripped - YAML comments are not part of the data model and have no JSON equivalent.
- Anchors and aliases are resolved to their referenced values before conversion.
Common Use Cases
API Testing
Author request payloads in the more readable YAML format, then convert to JSON for tools like Postman, Insomnia, or curl that send JSON request bodies.
Terraform Variables
Terraform accepts variable files in both HCL and JSON. Converting a YAML variable definition to JSON lets you feed it into a Terraform workspace that expects .tfvars.json without rewriting the structure.
jq & JSON Processing
jq is the standard command-line tool for querying and transforming JSON. Converting your YAML config or data file to JSON lets you use jq queries to extract specific fields without loading a YAML parser.
Database Import
Document databases like MongoDB, Firestore, and DynamoDB import data in JSON format. Converting a YAML data file to JSON gives you a structure that can be fed directly into mongoimport or the cloud console's bulk import tool.
YAML Features Without JSON Equivalents
YAML has a richer feature set than JSON. The following features are handled during conversion but cannot be represented in JSON:
- Multi-document YAML (multiple documents separated by
---) - only the first document is converted. - YAML tags (e.g.
!!binary,!!set) - converted to the closest JSON type, which may lose type information. - Integer and non-string keys - JSON object keys are always strings; integer YAML keys are converted to string keys.