Lesson 13 used json.loads to parse a string. The json module also has the file-handle versions:
import json
# Read a JSON file:
with open("data.json") as f:
data = json.load(f)
# Write a JSON file:
with open("out.json", "w") as f:
json.dump(data, f)loads takes a string, load takes a file. dumps returns a string, dump writes to a file. The s is for "string."
Exactly. Use loads / dumps when the JSON is in a variable or you're sending it over a network. Use load / dump when you have a file handle.
And what types map to JSON?
A bidirectional table:
| Python | JSON |
|---|---|
dict | object {...} |
list | array [...] |
str | string |
int, float | number |
True, False | true, false |
None | null |
A tuple becomes a list when serialized; a set has no JSON equivalent — convert to a list first. Datetime objects also have no native JSON form (lesson 20 covers ISO 8601 strings).
And the file format — is it human-readable?
By default, no — json.dump(data, f) writes everything on one line. Pass indent=2 for the pretty-printed multi-line form:
json.dump(data, f, indent=2) # human-readable, largerFor today: read a JSON file, mutate the dict, write it back.
json.load and json.dump — file versionsimport json
with open("data.json") as f:
data = json.load(f) # parse JSON file → Python value
with open("out.json", "w") as f:
json.dump(data, f) # serialize Python value → JSON fileloads / dumps vs load / dump — the s is for string| Function | Input | Output |
|---|---|---|
loads(text) | JSON string | Python value |
load(file_handle) | JSON file | Python value |
dumps(value) | Python value | JSON string |
dump(value, file_handle) | Python value | (writes to file) |
Python ↔ JSON:
| Python | JSON |
|---|---|
dict | object |
list, tuple | array |
str | string |
int, float | number |
True / False | true / false |
None | null |
Note: a tuple goes IN as JSON array but comes OUT as a list. Sets and datetimes are not JSON-native — convert sets to lists, format dates as ISO strings.
indentjson.dump(data, f, indent=2)
# Result file:
# {
# "a": 1,
# "b": [2, 3]
# }Use indent=2 for human-readable files, no indent for compact wire-format.
sort_keysjson.dump(data, f, sort_keys=True, indent=2)Useful for stable diffs in version control.
NaN / Infinity are non-standard. json allows them by default but other systems often reject. Use json.dump(..., allow_nan=False) if interoperability matters.json.dumps produces ASCII by default, escaping non-ASCII characters. Pass ensure_ascii=False to keep emoji and accented characters readable.json.load(open(...)) and json.dump(data, open(...)) — use them inside with blocks.json.loads(response.text) — the JSON arrives as a string.json.dumps(data) — shape it for transmission.Lesson 13 used json.loads to parse a string. The json module also has the file-handle versions:
import json
# Read a JSON file:
with open("data.json") as f:
data = json.load(f)
# Write a JSON file:
with open("out.json", "w") as f:
json.dump(data, f)loads takes a string, load takes a file. dumps returns a string, dump writes to a file. The s is for "string."
Exactly. Use loads / dumps when the JSON is in a variable or you're sending it over a network. Use load / dump when you have a file handle.
And what types map to JSON?
A bidirectional table:
| Python | JSON |
|---|---|
dict | object {...} |
list | array [...] |
str | string |
int, float | number |
True, False | true, false |
None | null |
A tuple becomes a list when serialized; a set has no JSON equivalent — convert to a list first. Datetime objects also have no native JSON form (lesson 20 covers ISO 8601 strings).
And the file format — is it human-readable?
By default, no — json.dump(data, f) writes everything on one line. Pass indent=2 for the pretty-printed multi-line form:
json.dump(data, f, indent=2) # human-readable, largerFor today: read a JSON file, mutate the dict, write it back.
json.load and json.dump — file versionsimport json
with open("data.json") as f:
data = json.load(f) # parse JSON file → Python value
with open("out.json", "w") as f:
json.dump(data, f) # serialize Python value → JSON fileloads / dumps vs load / dump — the s is for string| Function | Input | Output |
|---|---|---|
loads(text) | JSON string | Python value |
load(file_handle) | JSON file | Python value |
dumps(value) | Python value | JSON string |
dump(value, file_handle) | Python value | (writes to file) |
Python ↔ JSON:
| Python | JSON |
|---|---|
dict | object |
list, tuple | array |
str | string |
int, float | number |
True / False | true / false |
None | null |
Note: a tuple goes IN as JSON array but comes OUT as a list. Sets and datetimes are not JSON-native — convert sets to lists, format dates as ISO strings.
indentjson.dump(data, f, indent=2)
# Result file:
# {
# "a": 1,
# "b": [2, 3]
# }Use indent=2 for human-readable files, no indent for compact wire-format.
sort_keysjson.dump(data, f, sort_keys=True, indent=2)Useful for stable diffs in version control.
NaN / Infinity are non-standard. json allows them by default but other systems often reject. Use json.dump(..., allow_nan=False) if interoperability matters.json.dumps produces ASCII by default, escaping non-ASCII characters. Pass ensure_ascii=False to keep emoji and accented characters readable.json.load(open(...)) and json.dump(data, open(...)) — use them inside with blocks.json.loads(response.text) — the JSON arrives as a string.json.dumps(data) — shape it for transmission.Create a free account to get started. Paid plans unlock all tracks.