YAML to JSON: Convert Config Files When Your Code Needs JSON

Contents
Configuration files are often written in YAML. Structure is visible from indentation alone, and you can add comments freely, which makes YAML much easier for people to read and edit. But the moment you try to use those values inside JavaScript code or drop them straight into an API request body, you find out YAML won't work as-is.
Summary ① YAML is easy for people to read and write, but JavaScript objects and API request bodies expect JSON ② If you know the rules for moving from an indentation-based structure to a brace-and-bracket structure, you can convert it by hand ③ YAML with deep nesting or mixed lists is easy to get wrong by hand, so you need a step where you check the result with your own eyes
Why YAML config gets blocked in code
YAML represents hierarchy through indentation and puts values after a colon, which makes it easy to read. You can add comments (#) freely too, so it's widely used as a configuration file format.
The problem is that places like JavaScript's JSON.parse or a fetch request body don't understand YAML syntax. Those functions only parse JSON syntax: braces, brackets, and keys wrapped in double quotes.
So if you paste values from a config file straight into a test API request, or try to move them into frontend code as constants, you get a syntax error. The YAML itself is perfectly valid — it just fails anywhere JSON is expected.
How to check: conversion rules and verification
There are three rules when converting by hand. First, turn key: value into a form wrapped in double quotes. Second, wrap child items expressed by indentation in braces. Third, turn list items marked at the start of a line into bracket arrays.
| YAML notation | JSON notation |
|---|---|
| host: localhost | Keys and values wrapped in double quotes |
| Child items grouped by indentation | Wrapped in braces |
| List items at the start of a line | Converted to bracket arrays |
| true, false, null, numbers | Kept as-is without quotes |
The rules are simple, but once the hierarchy goes past two or three levels, matching up braces by hand isn't easy. If the structure has objects nested inside lists, the braces overlap and mistakes get even more likely.
Check it with our tool. Paste your YAML into the input box and the JSON result appears right below. Pick an indent width of 2 spaces, 4 spaces, or a single line (minified), then copy it and paste it into your code.
Free · no sign-up · convert to JSON right in your browser

Common places people get stuck
| Situation | Why it gets stuck |
|---|---|
| Structures with objects mixed into lists | Braces inside brackets overlap, so matching them by hand is hard |
| Strings containing colons or quotes | Without escape handling, you get a syntax error |
| Indenting with a mix of tabs and spaces | The hierarchy is interpreted differently, so it converts into the wrong structure |
| Leaving a comma after the last item | JSON doesn't allow a trailing comma after the last item |
The situations in this table don't jump out when you skim. You only see where the error is once you actually try to parse. Get in the habit of checking once after converting that the structure matches the original.
Wrapping up
YAML is a format that's easy for people to read and edit, and JSON is the format code and APIs understand. Turn colons into quotes, indentation into braces, and lists into brackets, and you can move it over by hand.
But when the hierarchy gets deep or lists and objects get mixed together, it's easy to slip up while matching braces. In that case, it's faster to use a tool that lets you verify the converted result right away.
Free · no sign-up · convert to JSON right in your browser
Frequently asked questions
Can all YAML syntax be converted to JSON?
Structures commonly used in config files — key-value pairs, nested maps, and lists — can be carried over to JSON as-is. YAML-specific syntax like anchors and references, however, has no equivalent in JSON, so you need to expand the values first, then convert.
Can I use the converted JSON as-is in an API request body?
If it's been converted to a standard JSON structure, you can put it straight into the body of a fetch or axios request. Just check separately that the field names and value types the API expects match the keys in the original YAML.
Should I pick 2-space or 4-space indentation?
The parsed data itself is the same regardless of indent width. If you plan to paste it into a codebase you're collaborating on, match whatever spacing that project uses. For places where whitespace doesn't matter, like an API request body, the single-line (minified) option is handier.
This article was adapted from our Korean original. Read the Korean original