When JSON to YAML makes sense
JSON is what APIs and programs emit; YAML is what people maintain by hand. You usually convert in this direction when data produced by a tool has to become a config file someone will edit:
kubectl get -o jsonoutput that should become a manifest in Git.- An API response you want to paste into a Helm values file or a GitHub Actions workflow.
- A
package.json-style settings object being moved to a YAML-based tool.
Because every JSON document is also valid YAML, the goal here is not validity but readability: indentation instead of braces, no quotes where they are not needed, and multi-line text shown as real lines.
How the output is written
The JSON is parsed losslessly and printed as block-style YAML with two-space indentation:
- Objects become mappings and arrays become
-sequences, in the original key order. - Empty objects and arrays are written inline as
{}and[]. - Strings containing line breaks are written as literal blocks (
|), so a certificate or a SQL query stays readable. - Numbers keep the exact digits from the source. A 20-digit ID is not rounded, though
1.50is written as1.5. - Long lines are never folded, so values are not split across lines.
Anchors and aliases are never generated: if the same object appears twice in the JSON, it is written out twice.
Quoting that protects your data
The trickiest part of YAML is that unquoted text can silently change type. A value that is a harmless string in JSON may be read as something else by a YAML parser, especially an older YAML 1.1 one such as PyYAML, Ruby’s Psych or Ansible.
To avoid that, the converter quotes any string a YAML 1.1 or 1.2 reader could mistype:
"NO","on","y"(booleans in YAML 1.1 — the Norway problem)."0755","1.0","1e3"(would become numbers)."null","~"and"2024-01-01".- Text containing
:or#, or starting with an indicator such as@,*,&,!or-.
Keys get the same treatment, which is why a key named n or on appears in quotes. Quoted output may look busier, but it reads back as exactly the JSON you started with.
Options and limits
This converter has no settings of its own. JSON contains no comments, so there is nothing to carry across; add comments to the YAML afterwards if you need them.
Invalid JSON is not guessed at. Trailing commas, single quotes or comments produce an error with a line and column; fix them in the JSON formatter, which can repair common mistakes, then convert. Duplicate keys are allowed through with a warning, and the last value wins, as in JSON.parse.
Round trips
Converting back with YAML to JSON returns the same data: the same keys in the same order, the same numbers and the same strings. Only formatting differs. That makes this pair handy for reviewing a JSON payload as YAML and then sending it on unchanged. Everything runs client-side, so you can do it with production data.
Examples
Kubernetes object from kubectl
Nested objects and arrays become an indented manifest you can commit, with key order unchanged.
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": { "name": "checkout", "labels": { "app": "checkout" } },
"spec": {
"replicas": 3,
"template": {
"spec": {
"containers": [
{ "name": "app", "image": "acme/checkout:1.8.2", "ports": [{ "containerPort": 8080 }] }
]
}
}
}
}apiVersion: apps/v1
kind: Deployment
metadata:
name: checkout
labels:
app: checkout
spec:
replicas: 3
template:
spec:
containers:
- name: app
image: acme/checkout:1.8.2
ports:
- containerPort: 8080
Values YAML 1.1 would misread
Each string that an older YAML parser would turn into a boolean, number or date is quoted; the real null stays unquoted.
{
"country": "NO",
"fileMode": "0755",
"version": "1.10",
"enabled": "on",
"released": "2024-01-01",
"nothing": null
}country: "NO"
fileMode: "0755"
version: "1.10"
enabled: "on"
released: "2024-01-01"
nothing: null
Multi-line strings
The embedded line breaks turn into a literal block scalar, which is far easier to edit than a string full of \n escapes.
{
"name": "nightly-report",
"query": "SELECT id, total\nFROM orders\nWHERE paid = true\n",
"notify": ["ops@example.com"]
}name: nightly-report
query: |
SELECT id, total
FROM orders
WHERE paid = true
notify:
- ops@example.com
Common errors and how to fix them
| Error | Cause | Fix |
|---|---|---|
Trailing comma before '}'Explained | The JSON has a comma after the last property, which JSON does not allow. | Remove the comma, or paste the input into the JSON formatter and use Fix it. |
Object keys must use double quotes, not single quotesExplained | The input is a JavaScript object literal or Python dict rather than strict JSON. | Replace single quotes with double quotes. For relaxed syntax, the JSON5 to JSON converter accepts it directly. |
Duplicate key "…" — most parsers keep only the last valueExplained | An object contains the same key twice. This is a warning: the YAML keeps the last value. | Remove the duplicate in the JSON if the earlier value was the one you wanted. |
Frequently asked questions
Why are some of my strings wrapped in quotes in the YAML?
They would otherwise be read as a different type, for example NO as false or 0755 as a number. The quotes keep them as text for both YAML 1.1 and 1.2 parsers.
Does the output use anchors for repeated objects?
No. Repeated data is written out in full each time, which is what most people expect from a config file and avoids surprising aliases.
Can it convert a top-level JSON array?
Yes. A top-level array becomes a YAML sequence, with each element starting with a dash at the left margin.
Will large integers lose precision?
No. The digits are copied exactly from the JSON source, so IDs longer than 16 digits stay intact in the YAML.
Is the JSON uploaded anywhere?
No. The page converts in your browser and sends nothing to a server, so it is safe for internal data.