0

I have a problem regarding JSON library in Python. I can't figure out a way to read data from json file that looks like this:

{"name": "LOTR", "author": "Tolkin"}{"name": "Aska", "author": "Ivo"}

because when I try to load data using this code:

    with open("json_books.txt","r") as file:
       json_data = json.load(file)

I get the following error:

json.decoder.JSONDecodeError: Extra data: line 1 column 37 (char 36)

I've looked it up and none of the solutions I found helped me. If anyone can help me with this one it would be much appreciated.

3
  • 5
    I believe that is not valid JSON. Could you add more details to your input example, for instance, more lines? Commented Mar 29, 2020 at 8:40
  • Your json file doesn't contain valid json data. You have to make sure it contains valid json before parsing. Commented Mar 29, 2020 at 8:42
  • Are you sure that your file looks like this? If yes, it cannot be parsed, period. But if your file is actually multi-line with one JSON entry per line (instead of everything mushed together), then you need to parse every line separately. You already know how to do that. Commented Mar 29, 2020 at 8:45

2 Answers 2

0

What you listed is not valid JSON. JSON must have a single list or object at top level -- this has two objects.

Perhaps the proper JSON would instead be:

[{"name": "LOTR", "author": "Tolkin"}, {"name": "Aska", "author": "Ivo"}]
Sign up to request clarification or add additional context in comments.

1 Comment

Okay so, the file format was wrong as everyone said, I've fixed it now, thanks!
0

You can read the file content as a string, extract the "char" number, which is an index, from the error message of the JSONDecodeError exception, and reparse the slice of the string up to that index as valid JSON, and parse the rest of the string in the same way, until it no longer raises an error:

import json
import re
s = '{"name": "LOTR", "author": "Tolkin"}{"name": "Aska", "author": "Ivo"}'
json_data = []
while True:
    try:
        json_data.append(json.loads(s))
        break
    except json.JSONDecodeError as e:
        match = re.match(r'Extra data: .*\(char (\d+)\)', str(e))
        if match:
            index = int(match.group(1))
            json_data.append(json.loads(s[:index]))
            s = s[index:]
        else:
            raise
print(json_data)

This outputs:

[{'name': 'LOTR', 'author': 'Tolkin'}, {'name': 'Aska', 'author': 'Ivo'}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.