Notes › Tech Notes › JSON to YAML Reorders Keys and Quotes Numbers: Output Side and 4 Indent Tiers

JSON to YAML Reorders Keys and Quotes Numbers: Output Side and 4 Indent Tiers

Measured on the output side only: digit-like keys are reordered by JSON.parse and leave with added quotes; the four indent tiers yield 41, 53, 77 and 35 bytes, yet the Tab tier breaks the round trip; 59 string samples split into 12 bare, 44 single-quoted and 3 double-quoted forms.

First, Draw the Accounting Line: Output Side Only

Everything measured here watches the output side: JSON goes in, YAML comes out, and the question is how the serializer writes key order, picks the indent, and adds quotes. The numbers come from loading the template application/index/view/tools/yaml-json.html script section verbatim into node:vm, attaching a minimal DOM stub, then calling the page's own jsonToYaml, scalarToYaml and numToYaml — pure Node, no browser opened. No script src hit anywhere in the page; both the parser and the serializer are written in-page, with zero external libraries involved.

Digit-like Keys Get Silently Reordered, Then Quoted

Feed in {"b":1,"2":2,"a":3,"10":10,"1":1,"c":4} and the measured output order is 1, 2, 10, b, a, c. Integer-looking keys are hoisted to the front in ascending order by the engine during JSON.parse itself; the output side merely stays faithful to the key order of the object it received. Those numeric keys also come back wearing quotes, in the shape '1': 1, in the shape '1': 1, seven lines and 38 bytes in total. The second sample {"9":1,"100":2,"10":3,"x":4,"-5":5} yields 9, 10, 100, x, -5: -5 does not count as an integer-looking key and lands after x. A sample built only from letter keys keeps its textual order line for line, six lines untouched. So the hint that "key order is preserved into YAML" only holds for inputs that contain no digit-like key names at all.

Four Indent Tiers: Tab Saves the Most, and the Round Trip Breaks on It

The same seven-line nested sample {a:{b:[1,{c:2}],d:{}},e:[]}, serialized through the four indent tiers, produces these byte counts:

Indent tierOutput bytesDifference ledgerRe-fed into this page
2 spaces41baselinepasses
4 spaces5312 more than the 2 tierpasses
8 spaces7724 more than the 4 tierpasses
Tab356 below even the 2 tierreports a tab error

Every difference is nothing but indent spaces. A six-level deep chain outputs 71, 113 and 197 bytes at the 2/4/8 tiers, differing by 42 and 84 — exactly the summed indent depth of 21 indent units multiplied by the width difference. The interface only offers four tiers, while the underlying code actually accepts any integer width from 1 to 12; passing indent 20 or indent 0 both clamp back onto the 2-tier product of 41 bytes. When tab and indent are passed together, Tab wins. Saving bytes is saving bytes, but feeding a Tab-tier product back into this page immediately reports "line 2: a tab character (Tab) appeared inside the indentation", and the round trip snaps at that tier. Multi-line strings are dragged in as well: the block scalar is rejected because its step contains a Tab, and newline-bearing strings are forced down into double quotes.

Output bytes per indent tier, same seven-line sample (lines unchanged) 41 53 77 35 2 spaces 4 spaces 8 spaces Tab tier re-feed: Tab error

Figure 1: the four-tier byte ledger — the Tab bar is the narrowest, yet it is the one that breaks the round trip.

The Quote Ledger: 12 Bare, 44 Single, 3 Double

Feed 59 string samples verbatim into scalarToYaml: 12 come out bare, 44 get single quotes, and only 3 get double quotes — the double-quote tier is reserved for strings containing a newline, a Tab, or a control character. The single-quote roster is wider than intuition suggests: the boolean words yes, on and null, the version number 11.10, the permission string 0644, the date 2026-10-23, the sexagesimal 12:30:00 all get wrapped; meanwhile 1.2.3 and http://x#y stay bare. Keys run through the same function: the dangerous keys on, 11.10 and a: b are all shielded by quotes at output, and out of eight lines and 70 bytes only the 1.2.3 key remains unquoted. plainSafe is a two-tier check: whenever either the default tier or the strict tier would rewrite the value, a quote is added. Even 1e3, which the strict tier can read back as a string, still gets wrapped, and passing strict into jsonToYaml changes nothing in the output. The subject of the promise "quotes are only added when ambiguity could arise" is this page's parser, not every YAML platform on earth.

Number Shapes: Precision Dies at the Door, the Output Stays Silent

1e21 goes in, and what comes out is 1.0e+21 — the mantissa gets a .0 appended and the exponent gets an explicit plus sign. Negative zero is written as plain 0. Infinity and NaN are written as .inf and .nan, reachable in this direction only through overflow: for example {"x":2.5e308} first parses into Infinity and only then lands as x: .inf. The heaviest number in this batch is 9007199254740993: by the time JSON.parse finishes it is already 9007199254740992, and neither the output side nor the error panel says a single word about it. The leading-zero string 0644 is shielded inside quotes, but the dropped last digit of a big integer goes unmentioned by anyone. The Chinese side of the ledger is clean: Chinese keys, Chinese values and emoji all stay bare; a six-line sample measures 52 characters against 69 UTF-8 bytes, and re-feeding it returns an identical document with 0 warnings. Now the volume ledger: the built-in JSON example, once beautified, weighs 2243 bytes and converts into 1465 bytes of YAML, a saving of 778; a JSON file with 5000 keys and 221671 bytes produces 261670 bytes across 30001 lines, with serialization taking 53.4ms and the re-feed parse 69.2ms. Inspecting that volume by eye still depends on the viewer side — the JSON viewer keeps its own rendering ceiling for oversized documents, and the old account lives at the measured rendering ceiling.

Round-Trip Identity: Five Forward Passes, Three Reverse Texts Change Faces

JSON→YAML→JSON, benchmarked against the post-parse values, is identical across five sample cases — in the dangerous-key and multi-line-string examples, those added quotes are precisely the round-trip guard rails. The duplicate-key case is only identical against the JS product, because the first k already vanished silently during parse. The single remaining break is still the Tab tier. The reverse direction, YAML→JSON→YAML: of five cases, three fail text identity — the flow-style collection a: [1, 2] is flattened into dash blocks, the double-quote tier degrades into a bare scalar, and the 4-space indent is re-indented by the default tier. Only the last case can be made byte-identical again by switching to the indent 4 tier. On this YAML and JSON conversion page, clicking the direction swap twice brings the product back to a byte-for-byte match with the first YAML.

Key reorder happens in JSON.parse, not on the output side JSON text order b 2 a 10 1 c keys after parse 1 2 10 b a c YAML output '1': 1 '2': 2 '10': 10 b: 1 59 string samples bare 12 single 44 double quotes 3 digit keys quoted: 7 lines 38 B

Figure 2: the three-stage key-order account on the left, the three quote tiers on the right.

The Error Panel and the yj-opts Option Ledger

The parse-failure panel is divided into five segments: the heading "parse failed, output stopped", the coordinates "line N column M", the message body, the source pointer plus "^ the problem starts here", and the suggestion line; the column number equals that line's indent plus 1. The outer shell for a broken JSON, "line N: JSON format error - ", is page wrapping, with the raw Node V8 engine text appended behind it (the wording inside a real browser was not measured in this batch); when a message carries no position, the panel falls back to line 1 column 1. Options persist under the yj-opts key of localStorage, where the default string holds 5 keys and 72 bytes in total. Writes happen at exactly two places, control change and direction switch — a debounced recompute never writes. The restore runs at the script tail, before the listeners are bound: when a stored seed restores into the Tab tier, the very first run already emits a Tab-tier product with the warning bar lit, while a corrupt seed silently falls back to all defaults.

Q: Why doesn't the YAML key order match the original JSON text?

When key names look like integers, the reorder completes at the JSON.parse stage, and every browser behaves the same at that step; pure letter keys keep insertion order. If key order carries business meaning, rename the digit-like keys before converting.

Q: The Tab tier saves bytes, why is it not recommended?

All it saves is indent width — the small gap of 35 bytes against 41 — and the price is that the product reports a tab error the moment you re-feed it into this page, while the YAML specification itself only recognizes space indentation. Choosing Tab also switches out a warning bar on the page.

Q: The last digit of a big integer changed — is that the converter's fault?

Not a fault of the output side. 9007199254740993 is already 9007199254740992 at the parse step, before the serializer ever sees it, so the output only writes what it was handed. If you must keep very long integers intact, handle them before they enter this page.

📎 Related articles

Charset Pool and Exclusions on the Random Password Page, Measured 2026-11-29 270° Clamps to 180°, Not -90°: The Angle Clamp and Canvas Ledger 2026-11-29 The PNG Quality Slider Spins for Nothing at Every Byte Size 2026-11-29 Text Set Operations Measured: Duplicates and Order 2026-11-28 HTML Beautify Deletes the Blank Line Inside pre 2026-11-28