JSON, YAML & Data Architecture: Schema Validation, Typing, Streaming & Serialization Best Practices
A comprehensive architecture guide to data interchange formats: overcoming JSON serialization pitfalls and BigInt truncation, bypassing the YAML Norway problem, type-safe runtime validation with Zod, and streaming multi-gigabyte JSONL pipelines.
1. JSON Serialization Realities & Edge Case Pitfalls
const safeStringify = (obj) => JSON.stringify(obj, (key, value) =>
typeof value === 'bigint' ? value.toString() : value
);Custom JSON replacer function safely converting BigInt values into strings.
2. YAML Mechanics: Anchors, Aliases & The "Norway Problem"
3. Runtime Schema Validation with Zod & TypeScript
4. Streaming Gigabyte Datasets: JSON Lines (JSONL)
import fs from 'fs';
import readline from 'readline';
async function processBigData(filePath) {
const fileStream = fs.createReadStream(filePath);
const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity });
for await (const line of rl) {
if (!line.trim()) continue;
const record = JSON.parse(line);
// Process individual record without filling heap memory:
await handleRecord(record);
}
}Processes arbitrary-sized data pipelines with fixed low memory footprint.
5. Data Format Selection Matrix
| Format | Typing / Schema | Human Readable | Optimal Use Case | Copy |
|---|---|---|---|---|
| JSON | Dynamic (JSON Schema optional) | Yes | Universal public web APIs and browser client-server communication | |
| JSONL (NDJSON) | One JSON document per line | Yes (line by line) | High-throughput event logging, analytics dumps, and bulk database migrations | |
| YAML | Dynamic (implicit typing, e.g. YAML 1.1 reads "no" as false) | Yes (most readable; comments allowed) | Human-edited configuration files (Kubernetes, CI/CD, Docker Compose) | |
| TOML | Typed values (strings, integers, dates) | Yes (comments allowed) | Application configs with explicit tables (Cargo.toml, pyproject.toml) | |
| Protocol Buffers (Protobuf) | Strict binary schema (.proto) | No (binary) | High-performance internal microservices (gRPC); payloads are typically several times smaller than JSON | |
| MessagePack | Binary JSON equivalent (schema-less) | No (binary) | Compact binary alternative to JSON for low-latency streaming such as WebSockets |
6. Terminal Data Transformation: jq Recipes
curl -s https://api.github.com/repos/facebook/react/issues | jq ".[ ] | {id: .id, title: .title, user: .user.login}"Extracts only necessary attributes into a clean, compact object list.
cat telemetry.json | jq ".events[].category" | sort | uniq -c | sort -nrExtracts array values, pipes through standard Unix tools, and counts frequency.
cat users.json | jq -r ".[] | [.id, .name, .email] | @csv"Formats arbitrary JSON collections into standard RFC-4180 CSV strings.
Related Free Online Tools
Put these techniques into practice immediately with our privacy-first browser utilities:
Generate strict TypeScript interfaces and type definitions from raw JSON samples.
Validate, format, and inspect JSON structures with collapsible syntax tree views.
Convert YAML configs to clean JSON format or validate YAML indentation syntax.