| Programmer's Python Data - JSON |
| Written by Mike James | |||
| Wednesday, 03 December 2025 | |||
Page 2 of 2
Multiple RecordsOne subtle point about JSON is that you cannot include more than one object in a file. This is not a problem in many situations. For example, reading and writing the configuration data of a program can usually be done in a single object. Things get trickier when you want to write something that you regard as a record to the file. If you want to write a JSON encoding of a single record there is no problem. The file will contain the JSON encoding of the record which can be read back as usual. If you want to write multiple records to the JSON file then things are more difficult. Simply writing one record after another will result in a file that isn’t a JSON file. For example, if you write the data in the example above: with path.open(mode="wt") as f:
json.dump(data,f)
json.dump(data,f)
and try to read it back then you will get a JSONDecodeError exception because a JSON file can only contain a single JSON object. There are at least two general solutions. The first sticks to the rule of one object per JSON file by turning the records into a list/array. For example: with path.open(mode="wt") as f:
json.dump([data,data],f)
Now we have a JSON array containing two records. When you read it back what you get is: [[1, 2, {'name': 'mike', 'id': 42, 'score': 3.145}],
[1, 2, {'name': 'mike', 'id': 42, 'score': 3.145}]]
and you can access each record as data[i]. This is a good solution, but it only works when you have all of the data ready to write to the file and can process the file in one go. It doesn’t allow you to add records to the end of the file, unless you change the ending of the file to insert the new record into the array. A simpler and more flexible solution is to write a file that has one JSON object per line. For example: with path.open(mode="wt") as f:
s=json.dumps(data)
print(s,file=f)
s=json.dumps(data)
print(s,file=f)
Now we are using dumps to create a JSON string and then print to write a line to the file. To decode the JSON we need to read it line-by-line and use loads to convert each line, so: with path.open(mode="rt") as f:
for line in f:
data= json.loads(line)
print(data)
displays: [1, 2, {'name': 'mike', 'id': 42, 'score': 3.145}]
[1, 2, {'name': 'mike', 'id': 42, 'score': 3.145}]
You can see that this works perfectly well, but you need to make sure that there are no embedded new line characters within the data being written.
JSON and DataclassesIt is fairly easy to use the one-record-per-line approach to saving and loading dataclasses using JSON. To save a dataclass to JSON all you need to do is convert it to a dict using the asdict function and write it to a line in the file. To read it back you then convert the dict back to a dataclass by creating a new instance. For example: import pathlib
import dataclasses
import json
@dataclasses.dataclass
class person:
name:str=""
id:int=0
score:float=0.0
me=person("mike",42,3.145)
path=pathlib.Path("myTextFile.json")
with path.open(mode="wt") as f:
s=json.dumps(dataclasses.asdict(me))
print(s,file=f)
with path.open(mode="rt") as f:
for line in f:
data= json.loads(line)
me1=person(**data)
print(me1)
Notice that asdict creates a deep copy so this should work for most dataclasses. In chapter but not in this extract
|
|||
| Last Updated ( Wednesday, 03 December 2025 ) |



