1

I am trying to load a json file that I have but i keep getting en error saying :

raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 27 column 1 (char 48 - 1512)

My code is as follows :

def main():
    with open('statement.json', 'r') as f:
        data = json.load(f)
    print (data)

if __name__ == "__main__":
    main()

JSON file is as follows:

{"File Size": "16281", "File Name": "apple.json"}
{"File Size": "128706", "File Name": "banana.json"}
{"File Size": "47366083", "File Name": "carrot.json"}
{"File Size": "7484", "File Name": "pear.json"}

2 Answers2

1

Input is not a valid JSON object, but newline-delimited JSON.

You should use :

with open('statement.json', 'r') as f:
    for line in f:
        data = json.loads(line)
        print (data)

to print each JSON document (each line).

norbjd
  • 10,166
  • 4
  • 45
  • 80
0

Your JSON is wrong. You have multiple JSON objects in one file. You need to put them all into a list.

You could have a top level list like this.

[
    {"File Size": "16281", "File Name": "apple.json"},
    {"File Size": "128706", "File Name": "banana.json"},
    {"File Size": "47366083", "File Name": "carrot.json"},
    {"File Size": "7484", "File Name": "pear.json"}
]

or create a whole new object and put the list inside, something like this

{
    "file_list":[
        {"File Size": "16281", "File Name": "apple.json"},
        {"File Size": "128706", "File Name": "banana.json"},
        {"File Size": "47366083", "File Name": "carrot.json"},
        {"File Size": "7484", "File Name": "pear.json"}
    ]
}
Sam
  • 454
  • 4
  • 18