SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
  • What is an API?
  • API Styles
  • Idempotency
  • Data Formats
  • REST, GraphQL, gRPC
  • Pagination, Filtering and Sorting
  • API Versioning
  • Error Handling and Status Codes
  • OpenAPI / Swagger
  • Bulk APIs and File Upload
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/APIs and Communication/API Design

Data Formats

JSON, XML, Protobuf and more: how data is written down to travel over a network.

⏱ 12 min readBeginnerMust KnowHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

A data format is the way we write data down so it can travel over a network or be saved. Both sides must use the same format.

  • Text formats (JSON, XML, YAML, CSV): people can read them. Bigger and slower.
  • Binary formats (Protobuf, Avro, MessagePack): small and fast. Not readable by eye.
  • Web APIs mostly use JSON. Service-to-service calls and big data pipelines often use Protobuf or Avro.

02Concept: What is it?

A program keeps data in memory as objects. A network cannot carry objects. It can only carry bytes. So we must change the object into bytes and back.

Serialize and deserialize
πŸ“¦Object in memory{ name, age }
→↓serialize
πŸ”€Bytes on the wireJSON / XML / Protobuf
→↓deserialize
πŸ“¦Object in memoryon the other side
Serialization (also called encoding or marshalling) = object to bytes. Deserialization = bytes to object.
🧠Two big questions
1. Can people read it? (text vs binary) 2. Is there a schema? (a written description of the fields and types that both sides share)
Text format
  • Human readable
  • Easy to debug with a browser or curl
  • Bigger and slower to parse
Binary format
  • Not readable without a tool
  • Small and fast
  • Usually needs a schema file

03Real-Life Example

🍡Posting a parcel
You want to send a bowl and a plate to a friend in another city. You cannot put them on the road as they are. You pack them (serialize) in a box. Your friend unpacks them (deserialize). A bigger box costs more to ship. A clever fold-flat box is smaller, but your friend must know how to open it.
Parcel
Data format
Packing the box
Serializing
Unpacking
Deserializing
A clear plastic box (you can see inside)
Text format: JSON, XML
A flat-pack box with an instruction sheet
Binary format with a schema: Protobuf, Avro
Weight of the box
Size on the network
Both sides know the folding rules
Shared schema
One record, three shapes
JSON
{"name":"Asha","age":28}
XML
<user><name>Asha</name><age>28</age></user>
CSV
name,age
Asha,28

04Why Do We Need It?

The format you choose changes speed, cost, safety and how easy it is to change the system later.

πŸš€Speed
Small messages travel faster and parse faster.
πŸ’°Cost
Less data means lower bandwidth and storage bills.
πŸ”Debugging
Readable text is easy to inspect when something breaks.
🀝Compatibility
A common format lets different languages and teams talk.
πŸ”„Change over time
A good format lets you add fields without breaking old readers.
πŸ›‘οΈSafety
A schema catches wrong or missing fields early.
Choose a format for who reads it (people or machines) and how often it is sent.

05Where Do We Use It?

FormatWhere it is common
JSONPublic web and mobile APIs, config files, NoSQL documents
XMLSOAP, older enterprise systems, RSS feeds, many document formats
YAMLConfig files: Kubernetes, CI pipelines, OpenAPI files
CSVSpreadsheets, data exports and imports, simple reports
ProtobufgRPC, internal service-to-service calls
AvroKafka and big data pipelines
MessagePack / BSONCompact JSON-like binary (BSON is used by MongoDB)
πŸ“±Mobile app ↔ server
JSON (easy) or Protobuf (saves data).
πŸ”§Service ↔ service
Protobuf over gRPC for speed.
πŸ“ŠData pipelines
Avro or Parquet files to move and store lots of data.

06Types / Parts / How It Works

Text formats

FormatLooks likeGood atWeak at
JSON{"id":1,"tags":["a","b"]}Simple, everywhere, nests data, easy in JavaScriptNo comments, no dates type, repeats keys, no built-in schema
XML<id>1</id>Strict, has schemas (XSD), attributes, mixed contentVerbose, harder to read and parse
YAMLid: 1Very readable, has comments, great for configIndentation mistakes, many surprising rules
CSVid,nameTiny for flat tables, opens in a spreadsheetNo nesting, no types, quoting rules

Binary formats

FormatIdeaSchema?Common use
Protocol Buffers (Protobuf)Fields get numbers, not names. Very compact.Yes (.proto file)gRPC, internal APIs
AvroSchema is stored with data or in a registry; row-based.Yes (JSON schema)Kafka, Hadoop
ThriftSimilar idea to Protobuf, with its own RPCYesSome older internal systems
MessagePackβ€œBinary JSON”: same data model, smaller bytesNoCaches, fast APIs
Parquet / ORCColumn-based files for analyticsYesData warehouses
Why Protobuf is small
JSON on the wire
"name": "Asha"name spelled out
"age": 28age spelled out
Protobuf on the wire
1 β†’ "Asha"field #1 = name
2 β†’ 28field #2 = age
The wire has field numbers, not names. Both sides learn the names from the shared .proto file.

Schema and evolution

Data lives longer than code. Old messages are stored, and old and new servers run at the same time. So a format must allow change.

ChangeBackward compatible?Notes
Add an optional fieldYesOld readers ignore it. New readers use a default for old data.
Remove an optional fieldUsuallyNever reuse its number or name.
Rename a fieldNo in JSON / Avro by name; yes in Protobuf (number stays)Plan carefully.
Change a field’s typeUsually NoMake a new field instead.
VisualizerSame record, four formatsEdit the fields. Watch the bytes change. Slide the record count to see the gap grow.
Start from an example
Show it as
{
  "name": "Asha",
  "age": 28,
  "city": "Pune",
  "active": true
}
Size on the wire for 1 record
JSON52 bytes
XML96 bytes
CSV38 bytes
Protobuf (computed from its wire format)β‰ˆ 18 bytes

JSON, XML and CSV sizes are exact (compact, no extra spaces, UTF-8). The Protobuf size is worked out from its encoding rules (1-byte tags, varints, length prefixes). Real messages may add small extras.

JSON vs XML
54%
JSON size as a share of XML
Protobuf vs JSON
35%
Protobuf size as a share of JSON
JSON. Text. Easy to read. The default for web APIs. Repeats field names in every record.

07How to Use It / Steps / Implementation

Choosing a format

  1. 1
    Who reads it?
    People and browsers β†’ JSON. Only machines β†’ consider a binary format.
  2. 2
    How much data, how often?
    Millions of small calls or large volumes β†’ binary saves money and time.
  3. 3
    Do you need a strict schema?
    If many teams share it, use Protobuf, Avro or JSON Schema, so mistakes show early.
  4. 4
    Is it a flat table?
    CSV is fine for exports. Nested data needs JSON or XML.
  5. 5
    Is it a config file?
    YAML or JSON (YAML allows comments).
  6. 6
    Plan for change
    Only add optional fields. Never reuse field numbers or names.
A Protobuf schema (.proto)
syntax = "proto3";

message User {
  string name   = 1;   // field numbers are what go on the wire
  int32  age    = 2;
  string city   = 3;
  bool   active = 4;
  // added later, safe for old readers:
  string email  = 5;
}
JSON with a header that says what it is
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{"name":"Asha","age":28,"city":"Pune","active":true}
πŸ’‘Content negotiation
A client can say what it can read with the Accept header, and the server says what it sent with Content-Type. Common values: application/json, application/xml, text/csv, application/x-protobuf.

08Key Terms to Know

TermMeaning in easy words
SerializationTurning an in-memory object into bytes (text or binary) to send or save
DeserializationTurning the bytes back into an object
SchemaA written description of the fields and their types
Text formatHuman readable, such as JSON, XML, YAML, CSV
Binary formatBytes that are not meant for reading, such as Protobuf and Avro
PayloadThe data part of a message
Backward compatibleNew code can read old data
Forward compatibleOld code can read new data (by ignoring fields it does not know)
Content-TypeAn HTTP header that says what format the body is in
UTF-8The standard way to turn text characters into bytes
Compression (gzip)A separate step that makes any message smaller by squeezing repeated patterns

09Pros, Cons and Trade-offs

FormatProsCons
JSONReadable, simple, universal, great toolingLarger, no native schema, no binary type
XMLStrict, schemas, namespaces, matureVery verbose, heavy to parse
YAMLMost readable, commentsWhitespace traps, slow, not for network APIs
CSVTiny and simple for tablesFlat only, no types, messy quoting
ProtobufVery small and fast, strong types, safe evolutionNot readable, needs a schema and tools
AvroCompact, schema evolution rules, great for streamsNeeds a schema registry in practice, less common in web APIs
βœ… Pros
  • The right format cuts bandwidth and CPU
  • A schema catches bugs before production
  • Gzip makes text formats much smaller
⚠️ Cons
  • Binary formats are harder to debug
  • Changing formats later is costly
  • A schema adds a build and sharing step
πŸ“Compression closes part of the gap
With gzip or brotli, JSON shrinks a lot, because the repeated field names compress well. Binary formats are still smaller and faster to parse, but the gap is not as big as the raw numbers.

10Common Problems and Mistakes

Common mistakes

  1. Changing a field’s meaning or type without a new field or version.
  2. Reusing a Protobuf field number after deleting a field. Old data is read as the wrong field. Reserve old numbers.
  3. Sending big numbers in JSON. JavaScript loses precision above 253. Send big ids as strings.
  4. Dates without a rule. Pick one format, such as ISO 8601 with a time zone (2026-12-01T10:30:00Z).
  5. Trusting the input. Always validate the data you parse. Huge or deeply nested input can crash a parser.
  6. Forgetting Content-Type and character encoding. Use UTF-8.
  7. Using CSV for nested data. It gets messy fast.
πŸ”’Number precision
Big integers and decimals (money) can be wrong in JSON.
πŸ•Dates and time zones
Three teams, three date formats, one bug.
πŸ”€Encoding
Not using UTF-8 garbles names and emojis.
🧨Unsafe parsing
Some parsers run code or expand huge input. Use safe settings.

11Real-World Examples

🌐Most web APIs
Use JSON because browsers and every language read it easily.
βš™οΈgRPC services
Use Protobuf to keep messages small and typed.
πŸ“‘Kafka pipelines
Often use Avro with a schema registry (general idea).
πŸ›οΈBanks and governments
XML and SOAP remain in many older systems.
☸️Kubernetes
Config is written in YAML.
πŸ“ˆBusiness reports
CSV exports open in a spreadsheet.
Mini case: one system, three formats
πŸ“±Mobile app
→↓JSON
πŸšͺAPI gateway
→↓Protobuf (gRPC)
🧩Service A
🧩Service B
→↓Avro
πŸ—„οΈData lake
Readable, public Compact, internal Pipeline
Use the best format for each link.

12Related Topics to Read Next

What is an API?β†’

Where data formats are used.

REST, GraphQL, gRPC→

REST and GraphQL use JSON; gRPC uses Protobuf.

API Styles→

The pattern of conversation that carries your data.

Checksums→

Checking that data was not damaged on the way.

HTTP / HTTPS→

Where Content-Type and Accept headers live.

Message Brokers→

Brokers like Kafka often carry Avro or Protobuf.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • βœ”Serialize: object β†’ bytes. Deserialize: bytes β†’ object.
  • βœ”Text: JSON (web default), XML (older systems), YAML (config), CSV (tables).
  • βœ”Binary: Protobuf (gRPC), Avro (Kafka, big data), MessagePack. Small, fast, schema.
  • βœ”Pick by: who reads it, how much data, need for a schema, need to evolve.
  • βœ”Evolve safely: add optional fields; never reuse numbers or change types.
  • βœ”Remember: UTF-8, ISO 8601 dates, big numbers as strings, always validate input.
  • βœ”One line: β€œJSON for people and public APIs, binary for machines and scale.”
← Previous
Idempotency
Next β†’
REST, GraphQL, gRPC