| Programmer's Python Data - JSON |
| Written by Mike James | |||||||||||||||||||||
| Wednesday, 03 December 2025 | |||||||||||||||||||||
Page 1 of 2 JSON is a very popular text data format but it is based on JavaScript can this work with Python? Find out what lies behind in this extract from Programmer's Python: Everything is Data. Programmer's Python
|
|
JSON |
Python |
|---|---|
|
object |
dict |
|
array |
list |
|
string |
str |
|
number (int) |
int |
|
number (real) |
float |
|
true |
True |
|
false |
False |
|
null |
None |
The similarity is so good that a Python programmer could read JSON as if it was PYON, i.e. PYthon Object Notation. If you regard JavaScript’s object as being a Python dict and its array as a Python list then you only have minor differences to deal with. In particular, the keys in a JSON object are always strings and when a Python dict is converted all of its keys are coerced to strings and when read back they are automatically strings. Keys also cannot be general objects, even if they are hashable.
Notice also that despite the fact that the JSON equivalent of a Python dict is called an object, JSON is not a general way of saving Python objects – it is good for saving arrays and dictionaries in all combinations, but not general objects. Typical uses of JSON are for data exchange and for user-editable configuration files.
The most important methods are dump and load which create and decode JSON respectively. Each is available in two versions – one works with a file and the other works with a string.
To create a JSON coded file or string from a suitable object, obj, use:
json.dump(obj, fp, *,
skipkeys=False,
ensure_ascii=True,
check_circular=True,
allow_nan=True,
cls=None,
indent=None, separators=None,
default=None,
sort_keys=False,
**kw)
and
json.dumps(obj, *,
skipkeys=False,
ensure_ascii=True, check_circular=True,
allow_nan=True,
cls=None,
indent=None, separators=None,
default=None,
sort_keys=False,
**kw)
The parameters that control the conversion are:
-
skipkeys if True will skip dictionary keys that are not str, int, float, bool or None, by default a TypeError exception is raised
-
ensure_ascii if True escapes ASCII characters, otherwise they are output as received
-
check_circular if False then the circular reference check for container types will be skipped and a circular reference will result in a RecursionError
-
allow_nan if False then any non numeric values nan, inf or -inf will throw a ValueError, if True the JavaScript equivalents re used.
-
indent can be a non-negative integer or string that includes indents in the JSON that makes it more human readable
-
separators specifies what white space should be included in separators to make the JSON more human readable
-
default can be set to a function that is called for objects that are not automatically convertible to return a JSON encoding or raise a TypeError
-
sort_keys if True then the output of dictionaries will be sorted by key.
For example, we can easily create a JSON file from a list of mixed data:
import pathlib
import json
path=pathlib.Path("myTextFile.json")
data=[1,2,{"name":"mike","id":42,"score":3.145}]
with path.open(mode="wt") as f:
json.dump(data,f)
print(json.dumps(data,indent=4))
This writes the JSON to a file and displays it using an indent to make it more readable;
[
1,
2,
{
"name": "mike",
"id": 42,
"score": 3.145
}
]
To read the data back into Python you can use the corresponding load or loads methods:
json.load(fp, *, cls=None, object_hook=None,
parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw) json.loads(s, *, cls=None, object_hook=None,
parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
where object_hook and object_pairs_hook can be used to define a function to process an object in the JSON. The object_hook function receives the object decoded as a dictionary and the object_pairs_hook receives the object as a list of (key,value) tuples. You can use them to customize how objects are decoded. The three possibilities are:
-
parse_float called with the string of every JSON float
-
parse_int called with the string of every JSON int
-
parse_constant called with '-inf', 'inf', 'NaN'
Using load you can read the data back from the previous example just as easily:
with path.open(mode="rt") as f:
data= json.load(f)
print(data)
which displays:
[1, 2, {'name': 'mike', 'id': 42, 'score': 3.145}]
which is what you would expect.
