[Python] Load broken JSON file

Discussão em 'Python' iniciado por Stack, Janeiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    there is a file for hundreds mB which supposed to be json file. Unfortunately, file was closed with error and json structure is not correct. Moreover, the file is one-line string.

    file ends with numerous ....\x00\x00\x00

    I tried many variants with reading json file with error control and manual decoding but still can not parse the data into pandas dataframe.

    last idea where I stoped: read the line by:

    with open('data.json', 'r', encoding='utf-8') as f:
    dt = f.read()


    and then I have to work with the line as string and tried to fix the json structure by finding the last correct block of data and add missed }] symbols and the end to let json.decode function work correctly.

    Is it any other variants like partially read file or auto fix errors in json structure?

    I tried such approach as well:

    import json
    import re

    class LazyDecoder(json.JSONDecoder):
    def decode(self, s, **kwargs):
    regex_replacements = [
    (re.compile(r'([^\\])\\([^\\])'), r'\1\\\\\2'),
    (re.compile(r',(\s*])'), r'\1'),
    ]
    for regex, replacement in regex_replacements:
    s = regex.sub(replacement, s)
    return super().decode(s, **kwargs)

    with open(r'data.json') as result:
    data = json.load(result, cls=LazyDecoder, strict=False)


    but it give error with delimiter JSONDecodeError: Expecting ',' delimiter: line 1 column 73900 (char 73899)

    Continue reading...

Compartilhe esta Página