I need to write a file that can be appended (and not overwritten!) and then to read this file. This is the code I wrote:
import io, json
def WriteJSON():
Taskslist = [["1Name", "1c:/path/file.ext", "1Some other text", 10, ["a", "b", "c"], ""],
["2Name", "2c:/path/file.ext", "2Test text", 20, ["a", "b", "c"], ""]]
with io.open('test.txt', 'a', encoding='utf8') as outfile:
str_ = json.dump(Taskslist, outfile)
def ReadJSON():
with io.open('test.txt', 'r', encoding='utf8') as readfile:
rf = json.load(readfile)
return rf
WriteJSON()
print(ReadJSON())
When I execute this program I get an error:
‘json.decoder.JSONDecodeError: Extra data: line 1 column 149 (char 148)’
But if I change the file to be written (with the ‘w’ option and not ‘a’ option) the file write and read is OK.
I saw the solution is to read line by line the JSON file but as I understands the json.dump writes the file by one stuck, so it’s one line….
My goal is to write a file than can be updated (append or delete data) and can be read any time the user asks for. I don’t really need JSON file, so any other solutions can be suggested.
2
Answers
The json format requires exactly one outermost object in the file — typically a list or a dictionary. So your requirement of being able to append to the file later on won’t work (unless you rewrite the file a little bit).
One common way around this is to not have a strict json file, but instead have a file where each line in the file is a separate little json record:
This format is often called "json lines".
JSON is a nested structure and can’t be appended to, but JSONL (JSON Lines) can. In the latter case write each JSON as a single newline-terminated line:
Output: