def parse_json ( filename ) : # Regular expression for comments comment_re = re . compile ( '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?' , re . DOTALL | re . MULTILINE ) with open ( filename ) as f : content = '' . join ( f . readlines ( ) ) ## Looking for comments match = comment_re . search ( content ) while match : # single line comment content = content [ : match . start ( ) ] + content [ match . end ( ) : ] match = comment_re . search ( content ) # Return json file return json . loads ( content )
python json file parse comment
Parse a JSON file First remove comments and then use the json module package Comments look like : // ... or / * ... * /
def parse_json(filename):
    """ Parse a JSON file
        First remove comments and then use the json module package
        Comments look like :
            // ...
        or
            /*
            ...
            */
    """
    # Regular expression for comments
    comment_re = re.compile(
        '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
        re.DOTALL | re.MULTILINE
    )

    with open(filename) as f:
        content = ''.join(f.readlines())

        ## Looking for comments
        match = comment_re.search(content)
        while match:
            # single line comment
            content = content[:match.start()] + content[match.end():]
            match = comment_re.search(content)

        # Return json file
        return json.loads(content)