Convert JSON to YAML

How to convert JSON to YAML without changing your data: what the conversion changes, the quoting and number pitfalls to check, and when to keep plain JSON.

Converting JSON to YAML is mostly a change of syntax. The data stays the same, but it is written differently: braces and brackets become indentation, and most quotation marks disappear. That makes configuration files easier for people to read and edit. The risk is in the details. A careless converter can change a string into a number or a boolean, and pasting the result into the wrong place can break the indentation. This review explains what the conversion changes, how to do it, and how to check that the meaning survived.

Is converting JSON to YAML right for your use?

Converting is a good choice when people will read or edit the result by hand. Typical cases are configuration files, CI pipeline definitions, deployment manifests and documentation examples. YAML is often easier to scan than JSON, and it allows comments, which JSON does not.

The main tradeoff is that YAML is a looser, more permissive format. Indentation carries meaning, and unquoted values can be read as types you did not intend. The conversion is only safe when the converter quotes values correctly and the file is handled as whitespace-sensitive text from then on.

Keep plain JSON in these situations:

  • Software exchanges the data with other software, such as API payloads, message queues or stored records, and no person needs to edit it.
  • The receiving system expects JSON or validates against a JSON-only schema.
  • The data will pass through channels that may alter whitespace, such as chat apps, rich-text editors or email.
  • You need strict, predictable parsing across many languages and libraries. JSON has fewer edge cases than YAML.

What the conversion actually changes

The conversion changes the stored characters, not just how the text looks on screen. A converter parses the JSON into its data model of objects, arrays, strings, numbers, booleans and null. It then writes that same structure out as YAML text. The YAML 1.2 specification was designed to be nearly a superset of JSON, so the two formats share a compatible data model. In most cases the conversion can preserve the data exactly.

Element | JSON | YAML output
Objects | Curly braces with quoted keys | Indented key: value lines; keys usually unquoted
Arrays | Square brackets | Indented lines starting with a dash, or inline brackets
Strings | Always double-quoted | Often unquoted; quoted only when needed to keep the value a string
Multi-line strings | One line with \n escapes | May use block styles (| or >) or stay quoted with escapes
Comments | Not supported | Supported, but none exist after conversion because JSON had none
Indentation | Cosmetic | Meaningful; spaces only, tabs are not allowed for indentation
Key order | Not significant per the JSON spec | Usually kept in input order, but some tools sort keys by default

The following pair is an illustrative example, not output captured from a specific tool. It shows one valid YAML rendering of a small JSON document. Different converters may choose different but equivalent styles, such as different list indentation or a quoted string instead of a block for the multi-line value.

{
  "name": "api",
  "version": "1.10",
  "enabled": true,
  "country": "NO",
  "ports": [8080, 8443],
  "notes": "line one\nline two"
}
name: api
version: "1.10"
enabled: true
country: "NO"
ports:
  - 8080
  - 8443
notes: |-
  line one
  line two

Several values in the example need quotes or special handling to keep their meaning:

  • version stays quoted. Unquoted, 1.10 would be read as the number 1.1, and the trailing zero would be lost.
  • country stays quoted. Parsers following YAML 1.1 rules treat unquoted NO, yes, on and off as booleans.
  • enabled is unquoted because it was a real boolean in the JSON.
  • notes uses a literal block. The |- indicator keeps the line break and strips the final newline, so the string matches the JSON value exactly.

How to convert JSON to YAML

On the command line, the Go-based yq tool converts between formats. Setting the input and output formats explicitly avoids relying on file-extension detection:

yq -p json -o yaml input.json > output.yaml

In Python, the standard json module and the PyYAML library can do the conversion. The sort_keys=False argument keeps the original key order, because PyYAML sorts keys by default. The allow_unicode=True argument writes non-ASCII characters directly instead of as escape sequences:

python3 -c 'import sys, json, yaml; print(yaml.safe_dump(json.load(sys.stdin), sort_keys=False, allow_unicode=True), end="")' < input.json > output.yaml

In JavaScript, the js-yaml library's dump function serializes a parsed object. Watch for large integers here. JSON.parse converts numbers to double-precision floats, so integers above 2^53 can silently change before the YAML is even written. If your data contains large IDs, use a parser that preserves big integers, or store those IDs as strings.

Online converters are convenient for small, non-sensitive snippets. A tool's appearance or marketing tells you nothing reliable about whether it stores or logs what you paste. Do not paste credentials, tokens or private configuration unless the tool documents how it handles data, or you can confirm it runs entirely in your browser.

Checking that the meaning survived

Formatting differences between tools are harmless. Changes to the data are not. The most reliable check is a round trip: convert the YAML back to JSON and compare it with the original, with keys normalized. With yq and jq, that looks like this:

diff <(jq -S . input.json) <(yq -p yaml -o json output.yaml | jq -S .)

Empty diff output means the data matches. The comparison also has limits. It confirms the result only for the parser used in the check. A system that reads YAML with different rules, such as an older YAML 1.1 parser, may still interpret unquoted values differently. If you know which parser will consume the file, test with that one.

Will the YAML stay usable after copying or sharing?

YAML depends on exact leading spaces, so it is more fragile in transit than JSON. Copy and paste between plain-text editors and terminals generally keeps whitespace intact. Rich-text editors, word processors, some chat apps and email clients may cause problems in several ways. They can collapse spaces, swap regular spaces for non-breaking spaces, insert tabs or apply smart quotes, and any of these can make the file invalid or change its structure. This article does not include tests of specific destinations. If you share YAML through a particular app, paste it into a code block or attach it as a file, then validate what the recipient actually receives.

For accessibility, YAML expresses nesting only through indentation. Screen readers do not always announce indentation unless they are configured to, whereas JSON's brackets are read out as characters. No screen reader testing was done for this article. If people using assistive technology need to understand the structure, consider also providing the JSON version, or describe the nesting in surrounding text.

For search and editing, a key and its value stay on one line in both formats, so text search works the same way. In YAML, however, a value's full path is spread across its parent lines, so finding a key does not show where it sits in the hierarchy. When editing YAML by hand, use an editor that shows whitespace and validates YAML. Indentation mistakes often produce a file that is still valid YAML but has a different structure, rather than an error.

What this review could not confirm

No specific converter, website or library version was tested for this article. The behavior described comes from the general design of the JSON and YAML formats and the commonly documented defaults of the tools named. Defaults can change between versions, so check the documentation for the exact version you use, especially for key sorting, quoting rules and which YAML specification version the tool follows.