# Serialization and Protobuf

> How a program turns its data into text or bytes to save or send, and back. How JSON Schema checks the shape, and why Protobuf is smaller.

**Course:** [JSON, YAML and File Formats](https://learn.benibix.com/learn/development-formats)  
**Time:** 12 min

**After this lesson you can:**

- Explain serialization in one sentence
- Read a small JSON Schema
- Say when Protobuf beats JSON

## How data leaves a program and comes back

Serialization turns the data in a program into text or bytes you can save or send. The other side reads it and builds the data back. That step is called deserialization. Emma's name and age take 24 bytes as JSON text, and just 8 bytes in Protobuf. Each saved game and each web request does this.

Inside a program, data is spread out in memory in a way only that program knows. A file or a network can only carry a line of bytes. So this step puts the data in a line, in a format both sides know.

JSON is the most common choice. People can read it, and each language can use it. In JavaScript, JSON.stringify does the packing and JSON.parse does the unpacking. Python has json.dumps and json.loads for the same two jobs.

Both sides must agree on the shape, not just the format. If one side sends age as text and the other wants a number, things break. JSON Schema is a way to write that shape down. It is itself written in JSON, and it lists each field, its type and which fields are required.

A tool can check any JSON against a schema and point to each mistake before the data is used. Code editors use schemas too, to warn you as you type a config file. The current version of JSON Schema is called 2020-12.

Protocol Buffers, or Protobuf, is a format from Google that packs data into compact bytes. You describe the data in a .proto file, giving each field a number. A tool then writes code for your language that packs and unpacks it. The bytes are small and fast to read, but people cannot read them.

Two mistakes are common. One is reading data from a stranger with no check. In some formats, that can even run an attacker's code. The other is changing a Protobuf field's number once it is in use. Then old messages land in the wrong field.

```json
{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer", "minimum": 0 }
  },
  "required": ["name"]
}
```
A JSON Schema: name must be text, age a whole number of 0 or more, and name is required.

```protobuf
message Person {
  string name = 1;
  int32 age = 2;
}
```
The same shape as a Protobuf message. The numbers 1 and 2 must never change once in use.

![On the left, a box called In memory holds name Emma and age 36. An arrow marked serialize leads to two forms in the middle: JSON text of 24 bytes, written out as name Emma and age 36 in curly brackets, and Protobuf bytes of 8 bytes, 0A 04 45 6D 6D 61 10 24. An arrow marked deserialize leads to Another program, which holds the same name and age. A note says a JSON Schema can check the text first.](https://learn.benibix.com/diagrams/dev2-serialization.svg)

### Example: Jack catches bad data early

Jack's shop gets 5,000 orders a day as JSON. Once a week, one arrives with the price as text, and the total comes out wrong.

He adds a JSON Schema that says price must be a number. Now the bad order is turned away at the door, with a message naming the field.

**Words in this lesson:** [Serialization](https://learn.benibix.com/term/development/serialization), [JSON Schema](https://learn.benibix.com/term/development/json-schema), [Protocol Buffers (Protobuf)](https://learn.benibix.com/term/development/protocol-buffers)

**Question:** What does serialization do?

**Answer:** It turns data in memory into text or bytes to save or send. Serialization lines data up as text or bytes. The other side reads it and builds the same data back.

---

Source: https://learn.benibix.com/learn/development-formats/serialization-and-protobuf
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
