skip to Main Content

I have .json documents generated from the same code. Here multiple nested dicts are being dumped to the json documents. While loadling with json.load(opened_json), I get the json.decoder.JSONDecodeError: Extra data: line 30 column 2 (char 590) like error for some of of the files whereas not for others. It is not understood why. What is the proper way to dump multiple dicts (maybe nested) into json docs and in my current case what is way to read them all? (Extra: Dicts can be over multiple lines, so ‘linesplitting’ does not work probably.)

Ex: Say I am json.dump(data, file) with data = {'meta_data':{some_data}, 'real_data':{more_data}}.

Let us take these two fake files:

{
    "meta_data": {
        "id": 0,
        "start": 1238397024.0,
        "end": 1238397056.0,
        "best": []
    },
    "real_data": {
        "YAS": {
            "t1": [
                1238397047.2182617
            ],
            "v1": [
                5.0438767766574255
            ],
            "v2": [
                4.371670270544587
            ]
        }
    }
}

and

{
    "meta_data": {
        "id": 0,
        "start": 1238397056.0,
        "end": 1238397088.0,
        "best": []
    },
    "real_data": {
        "XAS": {
            "t1": [
                1238397047.2182617
            ],
            "v1": [
                5.0438767766574255
            ],
            "v2": [
                4.371670270544587
            ]
        }
    }
}

and try to load them using json.load(open(file_path)) for duplicatling the problem.

The first file throws the error; copying and pasting however works fine

2

Answers


  1. You chose not to offer a
    reprex.

    Here is the code I’m running
    which is intended to represent what you’re running.
    If there is some discrepancy, update the original
    question to clarify the details.


    import json
    from io import StringIO
    
    some_data = dict(a=1)
    more_data = dict(b=2)
    data = {"meta_data": some_data, "real_data": more_data}
    
    file = StringIO()
    json.dump(data, file)
    file.seek(0)
    
    d = json.load(file)
    print(json.dumps(d, indent=4))
    

    output

    {
        "meta_data": {
            "a": 1
        },
        "real_data": {
            "b": 2
        }
    }
    

    As is apparent, over the circumstances you have
    described the JSON library does exactly what we
    would expect of it.

    EDIT

    Your screenshot makes it pretty clear
    that a bunch of ASCII NUL characters are appended
    to the 1st file.
    We can easily reproduce that JSONDecodeError: Extra data
    symptom by adding a single line:

    json.dump(data, file)
    file.write(chr(0))
    

    (Or perhaps chr(0) * 80 more closely matches the truncated screenshot.)

    If your file ends with extraneous characters, such as NUL,
    then it will no longer be valid JSON and compliant
    parsers will report a diagnostic message when they
    attempt to read it.
    And there’s nothing special about NUL, as a simple
    file.write("X") suffices to produce that same
    diagnostic.

    You will need to trim those NULs from the file’s end
    before attempting to parse it.
    For best results, use UTF8 unicode encoding with no
    BOM.


    Your editor should have settings for
    switching to utf8.
    Use $ file foo.json to verify encoding details,
    and $ iconv --to-code=UTF-8 < foo.json
    to alter an unfortunate encoding.

    Login or Signup to reply.
  2. You need to read the file, you can do both of these.

    data = json.loads(open("data.json").read())
    

    or

    with open("data.json", "r") as file:
        data = json.load(file)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search