skip to Main Content

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


  1. 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:

    [1,2,3,4,5]
    ["hello", "goodbye", "adios"]
    {"a": 1, "b": 2}
    

    This format is often called "json lines".

    Login or Signup to reply.
  2. 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:

    import 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 open('test.txt', 'a', encoding='utf8') as outfile:
            print(json.dumps(Taskslist), file=outfile)  # print adds the newline
    
    def ReadJSON():
        # Read each line, decode JSON and collect in a list.
        with open('test.txt', 'r', encoding='utf8') as readfile:
            return [json.loads(line) for line in readfile]
        return rf
    
    # Append a few times...
    WriteJSON()
    WriteJSON()
    WriteJSON()
    
    print(ReadJSON())
    

    Output:

    [[['1Name', '1c:/path/file.ext', '1Some other text', 10, ['a', 'b', 'c'], ''], ['2Name', '2c:/path/file.ext', '2Test text', 20, ['a', 'b', 'c'], '']], [['1Name', '1c:/path/file.ext', '1Some other text', 10, ['a', 'b', 'c'], ''], ['2Name', '2c:/path/file.ext', '2Test text', 20, ['a', 'b', 'c'], '']], [['1Name', '1c:/path/file.ext', '1Some other text', 10, ['a', 'b', 'c'], ''], ['2Name', '2c:/path/file.ext', '2Test text', 20, ['a', 'b', 'c'], '']]]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search