Modules and the standard library

INPUT · Slides

A shape for passing data about

01 / 07

The common tongue between applications

When a weather app shows you the weather, that content has arrived from a server somewhere. The same goes for a map app, or a journey planner.

What arrives is a run of characters. And the worldwide agreement on how to write them is JSON.

It looks remarkably like a Python dictionary, and in fact dictionaries and lists go straight to JSON. So there is almost no special conversion to learn.

02 / 07

Turn it into a string with dumps

json.dumps turns a Python value into a single string. The name is short for dump string.

Look at the type of what comes out and it is str. It is no longer a dictionary — it is just characters now.

import jsonperson = {"score": 80, "count": 3}text = json.dumps(person)print(text)print(type(text))

Result

{"score": 80, "count": 3}
<class 'str'>

03 / 07

Accented letters do not come out as they are

When the content holds a character outside plain ASCII, by default it is replaced with something like \u00e9. Nothing is broken — that is valid JSON too. It is a safety measure so the text can be read anywhere.

When you want the letters themselves, pass ensure_ascii=False. It is a keyword argument, from Chapter 3.

import jsonperson = {"name": "Renée", "score": 80}print(json.dumps(person))print(json.dumps(person, ensure_ascii=False))

Result

{"name": "Ren\u00e9e", "score": 80}
{"name": "Renée", "score": 80}

04 / 07

Bring it back with loads

The other direction is json.loads. It takes a JSON string and turns it back into a Python dictionary or list.

Once it is back, it is yours again. person["name"] and the rest of the usual dictionary writing all work.

import jsontext = '{"name": "Renée", "score": 80}'person = json.loads(text)print(person)print(person["name"])print(type(person))

Result

{'name': 'Renée', 'score': 80}
Renée
<class 'dict'>

05 / 07

Nesting makes the round trip too

A list inside a dictionary, a dictionary inside a list. However deep the nesting goes, it travels out and back unchanged — that is the strength of JSON.

You never have to take the structure apart and rebuild it. So even slightly complicated data can be handed over with confidence.

import jsonrecord = {"name": "Renée", "scores": [80, 90]}text = json.dumps(record, ensure_ascii=False)print(text)print(json.loads(text)["scores"][1])

Result

{"name": "Renée", "scores": [80, 90]}
90

06 / 07

The mark of having left Python

Two things do change how they are written. True becomes true, and None becomes null.

JSON is not only for Python, so Python spellings cannot be carried out of it. Run it back through json.loads, of course, and True and None return.

import jsonprint(json.dumps({"a": 1, "b": True, "c": None}))print(json.loads('{"a": 1, "b": true, "c": null}'))

Result

{"a": 1, "b": true, "c": null}
{'a': 1, 'b': True, 'c': None}

07 / 07

A shape the world already uses

JSON can be read and written in JavaScript, in Java, in Ruby, in Go. It is the common tongue for handing data to somebody writing in another language.

You could invent your own way of passing data, but then everyone else has to fall in behind you. Whatever you want to do, somebody has probably already made it — and what this chapter keeps saying holds for agreements as much as for tools.