Working with JSON and YAML: fix errors and convert safely
How to find a JSON syntax error fast, when to use YAML instead, and how to avoid the mistakes that change your data when converting.
Fixing invalid JSON
Most errors are one of four things: a trailing comma after the last item, single quotes instead of double quotes, a comment, or an unquoted key. The formatter shows the line and column where parsing stopped; the mistake is usually there or just before it.
Large integer IDs above 9,007,199,254,740,991 lose precision in JavaScript. Keep such IDs as strings.
JSON or YAML?
YAML is easier for people to edit and allows comments, which is why config files for Docker Compose, Kubernetes and CI use it. JSON is stricter and better for data passed between programs.
Converting YAML to JSON drops comments. Values like yes, no or 08 are quoted in YAML output so they stay text and are not read as booleans or numbers.
YAML mistakes that change your data
Tabs are not allowed for indentation in YAML; use spaces. A list item that starts with a dash must line up with its siblings, or it becomes part of the item above.
Unquoted values can change type: no and off may become false in older YAML tools, 1.10 becomes the number 1.1, and a version such as 3.10 loses its trailing zero. Quote anything that must stay exactly as written.
A quick checking routine
Paste the data into the JSON formatter first. If it is valid, convert the formatted output. If it is not, fix the reported line before doing anything else, because converters stop at the first error too.
Before committing a converted config, read it once. YAML is sensitive to indentation, so a value that ends up one level too deep or too shallow changes its meaning without causing an error.
Handling large numbers and IDs
JSON itself allows numbers of any size, but JavaScript stores them in a format that is exact only up to 9,007,199,254,740,991. Larger IDs, such as some database keys and social media IDs, are silently rounded when parsed. Ask the API for IDs as strings, or keep them in quotes in your own files. The JSON to CSV and CSV to JSON tools keep values as text for the same reason.