Flat keys for nested JSON, and the cases that trip flatteners up
Nested JSON is natural for programs and awkward for almost everything else. A spreadsheet has columns, not trees. Environment variables and many key-value stores take one level of names. A translation platform wants one string per key. Comparing two config files is far easier when each setting sits on one line. Flattening solves all of these by turning every leaf value into a single key that spells out the path to it: {"user": {"address": {"city": "London"}}} becomes {"user.address.city": "London"}.
Arrays get their positions in the path. With the default style, the first tag is tags.0 and the name of the second user is users.1.name. Bracket style writes tags[0] and users[1].name instead, which is the path syntax lodash, JavaScript and many form libraries read. A top-level array works too, giving keys that begin with the position. The separator between object keys can be a dot, a slash or an underscore, and the bracket style ignores the separator for positions.
Two cases trip up simpler flatteners. The first is empty containers. An empty object or list has no leaves, so a naive walk writes nothing for it and the key silently disappears; flatten and then unflatten, and settings: {} is gone. This tool keeps it as a value, "settings": {}, so the round trip is exact. The second is clashes. If a key already contains the separator, such as "a.b" next to an object a with a key b, both produce the key a.b. Rather than dropping one, the tool keeps both and names the clashing keys in the note, so you can pick a separator that tells them apart.
Values come through untouched. The JSON is read with a parser that keeps numbers as the digits you wrote and keys in their original order, so a 20-digit ID or a price written as 19.90 appears in the flat output exactly as it did in the input. The output keys are listed in the same order as the leaves appear in the original document.
Flattening is half of a round trip. JSON Unflatten rebuilds the nested structure from flat keys, turning runs of 0, 1, 2 back into lists. Use the same separator in both directions. Everything happens on this page, so private configuration and customer records never leave your device.