Serialization & Data Interchange Interview Questions
Converting Python objects to and from JSON and pickle, and the safety trade-offs between them.
Question 1: What is JSON handling in Python?
Ans
The json module converts between Python objects and JSON text/bytes representations using dumps, loads, dump, and load.
Example
python
import json
text = json.dumps({"name": "Amit", "age": 25})
data = json.loads(text)
print(data["name"])
Important Point
JSON supports a limited set of data types; arbitrary Python objects need custom serialization.
Question 2: What is serialization?
Ans
Serialization converts an in-memory object or data structure into a representation that can be stored or transmitted. JSON is common for interoperable data; pickle is Python-specific.
Example
python
import json
text = json.dumps({"id": 1, "name": "Amit"})
print(text)
Important Point
Never unpickle untrusted data; pickle can execute arbitrary code during deserialization.
Question 3: What is pickle?
Ans
pickle serializes and deserializes many Python object structures into a Python-specific binary representation.
Never load pickle data from an untrusted source because deserialization can execute arbitrary code.
Question 4: What is JSON vs pickle?
Ans
JSON is text-based, interoperable, and limited to JSON-compatible data. Pickle can represent many Python-specific objects but is Python-specific and unsafe for untrusted input.