Serialization and deserialization are two terms that come up constantly when working with JSON, but they can feel a bit abstract without a concrete example. This post explains both concepts directly, connecting them to what you’ve already seen in other posts on the blog about parse and stringify.
Serialization: from data structure to text
Serialization is the process of converting a data structure that exists in your program’s memory (an object, a list) into a text (or binary) format that can be stored or transmitted.
// javascript
const person = { name: "Ana", age: 30 };
const serializedText = JSON.stringify(person);
// '{"name":"Ana","age":30}'
Here, JSON.stringify() is the function that performs serialization: it takes a JavaScript object (which only exists in memory while the program runs) and turns it into a string, which can be saved to a file, sent in an HTTP request, or stored in a database.
Deserialization: from text back to a data structure
Deserialization is the reverse process: taking the serialized text and rebuilding the original data structure in memory.
// javascript
const receivedText = '{"name":"Ana","age":30}';
const person = JSON.parse(receivedText);
// { name: "Ana", age: 30 }
JSON.parse() is the deserialization function: it takes the string and rebuilds the JavaScript object, letting you access person.name normally in your code.
Why this process is necessary
Data structures like objects and lists only exist in a running program’s memory, they can’t be directly “sent” over a network or saved to a file, since memory is a concept specific to each process/language. Serializing converts that structure into a universal format (text, in JSON’s case) that any system, written in any language, can interpret, as long as it also “speaks” the same format.
It’s not exclusive to JSON
Even though this blog is about JSON, it’s worth mentioning that serialization/deserialization is a broader concept, one that applies to other formats too:
- XML: serializing an object to XML, deserializing XML back into an object
- Binary formats: Protocol Buffers, MessagePack, Avro, which serialize into binary formats that are more compact than text, but less readable by humans
- Language-native serialization: Python has
pickle, Java hasSerializable, formats specific to each language, which usually aren’t interoperable across different languages (unlike JSON, which is universal)
Equivalent terminology across languages
The concept is the same, but the function names change.
| Language | Serialize | Deserialize |
|---|---|---|
| JavaScript | JSON.stringify() | JSON.parse() |
| Python | json.dumps() | json.loads() |
| PHP | json_encode() | json_decode() |
| Java (Jackson) | writeValueAsString() | readValue() |
| Go | json.Marshal() | json.Unmarshal() |
Regardless of the specific name, the direction is always the same: serializing goes from memory toward text, deserializing does the reverse.
Where this shows up day to day
- APIs: the server serializes the response into JSON, the client deserializes it to use the data
- Configuration files: the application deserializes the JSON file at startup, and can serialize it back if it needs to save changes
- NoSQL databases: many databases automatically serialize objects when saving, and deserialize them when querying
If you want to check whether a serialized text is in the correct format before trying to deserialize it, paste the content into JSON Validator.