flashman
← All guides

Convert YAML scalars without a surprise null

Convert YAML to JSON without mistaking null: non-finite scalars collapse, merge keys stay literal, and JSON-to-YAML quotes strings that would change type.

2026-10-21 · 8 min read

  • yaml
  • json
  • tooling

Flashman's YAML page has two directions. YAML to JSON parses with yaml and prints JSON.stringify at two spaces plus a trailing newline. JSON to YAML parses with JSON.parse and prints YAML.stringify, which also ends in a newline. A box that trims to empty returns an empty string and does not call either parser.

The useful review is the token that survived, not the indentation. Comments, anchors, and a few scalars do not come back as the characters you typed.

Name the null you are looking at

.inf, -.inf, and .nan are real non-finite numbers after parse. The JSON panel cannot print them, so each one becomes null. A comment-only document parses as null and prints null plus a newline. A whitespace-only box never reaches the parser, so the panel stays blank. Inside a map, null, Null, NULL, and ~ are JSON null because that is the scalar.

A merge key is a fourth shape that looks empty and is not. <<: *anchor stays a key whose name is <<. The alias target is the value of that key. x from the anchor is not copied onto the parent. An ordinary alias is copied: both names appear in the JSON with the same contents.

  • Diff .inf against the null it becomes.
  • Diff a comment-only file against the word null.
  • Expect a whitespace-only box to stay blank.
  • Expect << to remain a key.

Read numbers and booleans in the JSON schema this page uses

0o10 is 8. 0x10 is 16. 010 is 10. 08 is 8. 0b10 stays the string 0b10. 1.10 becomes 1.1. The integer 9007199254740993 is printed as 9007199254740992, because the value passed through a JavaScript number. true, True, TRUE, false, and False are booleans. yes, Yes, on, On, n, and y stay strings.

A key written as ~ becomes an empty-string key. Duplicate keys throw Map keys must be unique and quote the line. A tab used as indentation throws Tabs are not allowed as indentation. Two documents throw and tell you to use YAML.parseAllDocuments(). An alias written as a bare * throws Alias cannot be an empty string. Each of those failures is prefixed with Error: and Copy will copy the whole message.

Quote on the way back to YAML

JSON to YAML keeps key order and drops nothing that JSON.parse can hold. Strings that would change type if they were plain scalars are quoted. true, null, 1.10, 08, ~, True, and 1.0 are quoted. yes, Yes, NO, on, and n are left plain, and in this parser those plain words are still strings on the way back to JSON.

A JSON object that is not strict JSON fails in JSON.parse. {a: 1} returns Error: Expected property name or '}' in JSON at position 1 (line 1 column 2). Trailing commas fail the same way. Fix the JSON before you judge the YAML.

Record the pair you compared

Paste the smallest document that shows the scalar, copy the JSON, and diff the two. For a merge, include the anchor and the << line so a reviewer can see that x did not move. For a large integer, write both digit strings in the note.

Use the YAML tool for the conversion, the JSON formatter for a second look at the panel, diff for the source and the output, and the regex tester when you are searching a repo for .inf, <<, or a repeated key. The page is a viewer. The application loader still needs its own schema.

Try these tools