liveBook · Manning

Json

As explained in The Quick Python Book, Fourth Edition

JSON is a Python library for encoding and decoding JSON data.

JSON (JavaScript Object Notation) is a lightweight text format for structured data exchange. It uses key-value structures and ordered arrays, making complex data readable and widely transferable across languages. section 22.3.1 section 22.3

Use Python’s standard-library json module to translate between JSON and native Python values:

  • Decode a string with json.loads() or read a file-like object with json.load(). section 22.3.1
  • Encode a Python value to a string with json.dumps() or write it to a file with json.dump(). section 22.3.1
  • Map JSON objects to dict, arrays to list, strings to str, numbers to int or float, true and false to True and False, and null to None. section 22.3.1
  • Pretty-print nested data with json.dumps(value, indent=2), or inspect it with pprint for easier reading. section 22.3.1

Decode an API response

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) print(weather["current"]["temperature_2m"]) # 16.9 requests responses can also expose the decoded value directly with response.json(). Save a decoded value with json.dump(weather, outfile), or obtain an encoded string with json.dumps(weather). section 22.3.1

Table 19.6 Modules supporting internet protocols and formats (view table figure)

Module

Description

socket, ssl

Low-level networking interface and SSL wrapper for socket objects

Email

Email and MIME handling package

Json

JSON encoder and decoder

Mailbox

Manipulate mailboxes in various formats

Mimetypes

Map filenames to MIME types

base64, binhex, binascii, quopri, uu

Encode/decode files or streams with various encodings

html.parser, html.entities

Parse HTML and XHTML

xml.parsers.expat, xml.dom, xml.sax, xml.etree.ElementTree

Various parsers and tools for XML

cgi, cgitb

Common Gateway Interface support

Wsgiref

WSGI utilities and reference implementation

urllib.request, urllib.parse

Open and parse URLs

ftplib, poplib, imaplib, nntplib, smtplib, telnetlib

Clients for various internet protocols

Socketserver

Framework for network servers

http.server

HTTP servers

xmlrpc.client, xmlrpc.server

XML-RPC client and server

Browse the surrounding internet-format modules to place JSON beside networking, MIME, URL, XML, and HTTP tools. section 19.1.4

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains JSON structures, type mappings, decoding, encoding, pretty printing, and newline-delimited records without covering parser internals.
Examples
Uses weather and Chicago API responses, nested temperature lookup, pretty printing, JSON files, and newline-delimited weather records.
What it assumes
Assumes general Python and HTTP knowledge while defining JSON structures and default conversions explicitly.
What you can do after
Apply JSON via Python’s json module to decode API responses, inspect nested values, and read or write JSON files.
How it connects
Connects JSON with Python dictionaries, Python lists, REST APIs, HTTP/HTTPS requests, and XML.

Also explained in

Compare how 4 books explain json →

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.