I have seen the terms "deserialize" and "serialize" with JSON. What do they mean?
-
1Does this answer your question? [What is serialization?](https://stackoverflow.com/questions/633402/what-is-serialization) – codeforester Apr 24 '20 at 08:48
-
@codeforester not really. JSON is not mentioned there. Also, we are 10 years late – Pablo Mar 28 '23 at 17:37
4 Answers
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).
When transmitting data or storing them in a file, the data are required to be byte strings, but complex objects are seldom in this format. Serialization can convert these complex objects into byte strings for such use. After the byte strings are transmitted, the receiver will have to recover the original object from the byte string. This is known as deserialization.
Say, you have an object:
{foo: [1, 4, 7, 10], bar: "baz"}
serializing into JSON will convert it into a string:
'{"foo":[1,4,7,10],"bar":"baz"}'
which can be stored or sent through wire to anywhere. The receiver can then deserialize this string to get back the original object. {foo: [1, 4, 7, 10], bar: "baz"}
.
-
7@kennytm - I am trying to wrap my head around sending stuff over the wire. Regardless of whether I use binary encoding or use json, xml or proto buf - the data always has to be in bytes before they can be sent over the wire. Is that true? – Nirmal Mar 31 '17 at 14:57
-
1
-
2A mnemonic device I use to remember the difference is that "Serialization turns objects into serial numbers" – Janac Meena Jul 04 '19 at 15:39
-
4
-
1@kennytm, I'm wandering too _so why don't we just `"{foo: [1, 4, 7, 10], bar: "baz"}"`?_. Is quoting `foo` and `bar` a typo in your answer, a convention, a necessity or what? – Enlico Jan 23 '20 at 16:33
-
4@EnricoMariaDeAngelis JSON Properties must be in quotes. It is a necessity - check out the first example on the wiki: https://en.wikipedia.org/wiki/JSON – Cloud Jan 24 '20 at 13:47
-
serialization is converting into stream of bytes and when we reconstruct we need some format or standard like JSON or XML. That's why we use serialization with JSON or XML. Is it right? @kennytm – Muhammad Faizan Fareed Feb 23 '20 at 14:15
Serialize and Deserialize
In the context of data storage, serialization (or serialisation) is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer) or transmitted (for example, across a network connection link) and reconstructed later. [...]
The opposite operation, extracting a data structure from a series of bytes, is deserialization.
JSON
JSON (JavaScript Object Notation) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other serializable values). It is a common data format with diverse uses in electronic data interchange, including that of web applications with servers.
JSON is a language-independent data format. It was derived from JavaScript, but many modern programming languages include code to generate and parse JSON-format data. JSON filenames use the extension .json.
Explained using Python
In Python serialization does nothing else than just converting the given data structure into its valid JSON pendant (e.g., Python's True
will be converted to JSON's true
and the dictionary itself will be converted to a string) and vice versa for deserialization.
Python vs. JSON
You can easily spot the difference between Python and JSON representations in a side-by-side comparison. For example, by examining their Boolean values. Have a look at the following table for the basic types used in both contexts:
Python | JSON |
---|---|
True |
true |
False |
false |
None |
null |
int , float |
number |
str (with single ' , double " and tripple """ quotes) |
string (only double " quotes) |
dict |
object |
list , tuple |
array |
Code Example
Python builtin module json
is the standard way to do serialization and deserialization:
import json
data = {
'president': {
"name": """Mr. Presidente""",
"male": True,
'age': 60,
'wife': None,
'cars': ('BMW', "Audi")
}
}
# serialize
json_data = json.dumps(data, indent=2)
print(json_data)
# {
# "president": {
# "name": "Mr. Presidente",
# "male": true,
# "age": 60,
# "wife": null,
# "cars": [
# "BMW",
# "Audi"
# ]
# }
# }
# deserialize
restored_data = json.loads(json_data) # deserialize
Sources: realpython.com, geeksforgeeks.org

- 13,026
- 8
- 71
- 88
-
1
-
Simple and very clear answer. Also explains the subtle distinction between the python dict and JSON object types. – Banty Feb 16 '23 at 10:24
Explanation of Serialize and Deserialize using Python
In python, pickle module is used for serialization. So, the serialization process is called pickling in Python. This module is available in Python standard library.
Serialization using pickle
import pickle
#the object to serialize
example_dic={1:"6",2:"2",3:"f"}
#where the bytes after serializing end up at, wb stands for write byte
pickle_out=open("dict.pickle","wb")
#Time to dump
pickle.dump(example_dic,pickle_out)
#whatever you open, you must close
pickle_out.close()
The PICKLE file (can be opened by a text editor like notepad) contains this (serialized data):
€}q (KX 6qKX 2qKX fqu.
Deserialization using pickle
import pickle
pickle_in=open("dict.pickle","rb")
get_deserialized_data_back=pickle.load(pickle_in)
print(get_deserialized_data_back)
Output:
{1: '6', 2: '2', 3: 'f'}

- 664
- 1
- 7
- 14
-
8
-
3This is original question: What is deserialize and serialize in JSON? I used Python's pickle module to demonstrate the idea. I have used a tool to explain an idea. You are focusing on the tool more than the idea. – Asif Dec 24 '19 at 19:01
Share what I learned about this topic.
What is Serialization
Serialization
is the process of converting a data object into a byte stream
.
What is byte stream
Byte stream
is just a stream of binary data. Because only binary data can be stored or transported.
What is byte string vs byte stream
Sometime you see people use the word byte string
as well. String encodings of bytes are called byte strings
. Then it can explain what is JSON as below.
What’s the relationship between JSON and serialization
JSON
is a string format representational of byte data. JSON is encoded in UTF-8. So while we see human readable strings, behind the scenes strings are encoded as bytes in UTF-8.

- 2,418
- 8
- 35
- 62