Home > Blockchain >  Inconsistent error: json.decoder.JSONDecodeError: Extra data: line 30 column 2 (char 590)
Inconsistent error: json.decoder.JSONDecodeError: Extra data: line 30 column 2 (char 590)

Time:01-11

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.

CodePudding user response:

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.

CodePudding user response:

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)
  • Related