Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 39 ยท JSON in Depth

JSON Pitfalls and Variants

Avoid common JSON mistakes, then meet its variants: JSON Lines for streams, JSONC and JSON5 for config files, and binary relatives like BSON and MessagePack.

JSON is simple, and that simplicity hides a few traps. It is also deliberately minimal, so a family of variants has grown up to fill the gaps: comments, trailing commas, streaming and binary encodings. This lesson finishes the JSON chapter with both.

Pitfalls worth memorizing

  1. Duplicate keys. {"a": 1, "a": 2} is technically allowed, but parsers disagree. JavaScript keeps the last value; others may keep the first or fail. Never rely on it.
  2. Key order. The standard says objects are unordered. JavaScript preserves insertion order, except that integer-like keys come first in ascending order. Do not use order to carry meaning; use an array.
  3. __proto__ keys. JSON.parse('{"__proto__": {"x": 1}}') creates an own property named __proto__; merging that object into another with a naive deep-merge can pollute prototypes. Validate and use safe merge code.
  4. Large numbers and precision (see JSON types and numbers).
  5. Unicode surprises. Lone surrogate escapes such as \ud800 are accepted by some parsers and rejected by others. Normalize and validate text at the edges.
  6. Huge or deeply nested documents. A hostile payload can exhaust memory or the call stack. Put size and depth limits in front of your parser.
Node.js Duplicate keys, key order and __proto__

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The merge example prints polluted? true: assigning target['__proto__'] = ... changed the shared Object.prototype. Lesson: when copying keys from parsed data, skip __proto__, constructor and prototype, or use Object.create(null) and Map.

JSON Lines (NDJSON)

A normal JSON file is one big value, so you must load all of it. JSON Lines (also called NDJSON) puts one complete JSON value on each line, separated by \n. It suits logs, exports and streams: you can append a record without rewriting the file and process one line at a time.

Node.js Reading and writing JSON Lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Because JSON.stringify never emits a raw newline inside a value, each record stays on one line.

JSONC: JSON with comments

JSONC allows // and /* */ comments (and often trailing commas). VS Code settings, tsconfig.json and many tools read it. It is not standard JSON, so JSON.parse rejects it. A tiny comment stripper shows the idea; this is a stand-in that ignores comment markers inside strings only approximately, so use a real JSONC parser in production.

Node.js A naive JSONC reader (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

JSON5

JSON5 extends JSON toward JavaScript syntax: comments, unquoted keys, single-quoted strings, trailing commas, hexadecimal numbers, +1, .5, Infinity and NaN. It is friendlier for hand-edited config, but requires a JSON5 library, and its files are not valid JSON.

JSON5
{
  // comments are fine
  name: 'example',
  retries: +3,
  ratio: .5,
  tags: ['a', 'b',],
}

Other relatives

Format Idea
JSON Lines / NDJSON One JSON value per line, for streams and logs
GeoJSON JSON structure for geographic features
JSON-LD JSON with linked-data vocabulary for search engines
BSON Binary JSON used by MongoDB, with dates and binary types
MessagePack, CBOR Compact binary encodings of the JSON data model

Pick plain JSON for interchange, JSON Lines for streams, and JSONC or YAML for hand-edited config.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main advantage of JSON Lines over a JSON array?

    Each line is a complete value, so you can stream and append without loading everything.
  2. 2. Which statement about JSONC is true?

    JSONC adds comments, so standard parsers reject it.
  3. 3. What is a risk of copying keys from parsed JSON into another object blindly?

    Keys such as proto can alter shared prototypes in naive merge code.
  4. 4. Which relative is a binary encoding of the JSON data model?

    MessagePack (like CBOR and BSON) is binary.

Summary

  • Avoid duplicate keys, order-dependent logic and blind merges of parsed data.
  • Add size and depth limits before parsing hostile input.
  • JSON Lines suits logs and streams; JSONC and JSON5 add comments for config.
  • Binary relatives trade readability for size and speed.

Next: JSON Schema: an introduction