What is the difference between pickle and json in Python?

87 views

What is the difference between pickle and json in Python?

What is the difference between pickle and json in Python?

solveurit24@gmail.com Changed status to publish February 13, 2025
0
  • pickle:
    • Serializes (pickles) Python objects into a binary format.
    • Can handle complex Python objects (e.g., custom objects, NumPy arrays).
    • Less human-readable.
  • json:
    • Serializes objects into JSON format (text-based).
    • Suitable for data exchange between systems.
    • Human-readable and widely used.

Example:

import pickle
import json

data = {"name": "Alice", "age": 30}

# Pickle
with open('data.pkl', 'wb') as file:
    pickle.dump(data, file)

# JSON
with open('data.json', 'w') as file:
    json.dump(data, file)

solveurit24@gmail.com Changed status to publish February 13, 2025
0
You are viewing 1 out of 1 answers, click here to view all answers.