liveBook · Manning

Serialization

As explained in The Quick Python Book, Fourth Edition

Serialization is the transformation of an in-memory data structure or object into a representation suitable for storage or transmission.

Serialization transforms in-memory data into a representation suitable for storage or transmission. JSON represents data as text and becomes a sequence of bytes when sent across a network; deserialization converts that representation back into Python objects. section 22.3.1

Python's JSON interface separates the main serialization and deserialization operations:

  • Use json.loads() to transform a string representation into a Python object, or json.load() to read JSON from a file-like object. section 22.3.1
  • Use json.dumps() to return a JSON string, or json.dump() to write JSON directly to a file. section 22.3.1
  • By default, JSON objects become dictionaries, arrays become lists, strings become str, numbers become int or float, and null becomes None. section 22.3.1
  • Pretty-print serialized data with pprint or json.dumps(..., indent=2) when readability matters. section 22.3.1

Weather data

Retrieve JSON with requests, decode it with weather = json.loads(response.text), and access a nested value with weather['current']['temperature_2m']. The requests response also provides response.json() as a shorter alternative. Save the resulting object with json.dump(weather, outfile), or obtain a string with json.dumps(weather). section 22.3.1

import json
import requests
response = requests.get("https://api.open-meteo.com/v1/forecast?latitude
=41.882&longitude=-87.623&current=temperature_2m,wind_speed_10m")
weather = json.loads(response.text)
weather
 
{'latitude': 41.879482,
 'longitude': -87.64975,
 'generationtime_ms': 0.025987625122070312,
 'utc_offset_seconds': 0,
 'timezone': 'GMT',
 'timezone_abbreviation': 'GMT',
 'elevation': 185.0,
 'current_units': {'time': 'iso8601',
  'interval': 'seconds',
  'temperature_2m': '°C',
  'wind_speed_10m': 'km/h'},
 'current': {'time': '2024-09-02T02:15',
  'interval': 900,
  'temperature_2m': 16.9,
  'wind_speed_10m': 13.8}}
 
weather['current']['temperature_2m']
 
16.9

A runnable weather API example shows network retrieval, JSON decoding, nested dictionary access, and the resulting Python data structure. section 22.3.1

Pickling is another serialization option, but it is neither especially fast nor space-efficient compared with JSON. It is also insecure: loading malicious pickle content can execute arbitrary code, so avoid it when other people could alter the file. section 13.8.1

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains JSON encoding, decoding, default type mappings, formatting, file handling, and pickle limitations without implementation internals.
Examples
Uses weather API responses, nested weather dictionaries, pretty-printed output, JSON files, and newline-delimited weather reports.
What you can do after
Apply JSON encoding and decoding with Python's json module and requests, while recognizing when pickle is unsafe or inefficient.
How it connects
Connects serialization with JSON, deserialization, dictionaries, files, HTTP APIs, requests, and pickling.

Also explained in

Compare how 24 books explain serialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.