Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix json_cleaner #22

Merged
merged 1 commit into from
Dec 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions json_cleaner/json_cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ def get_json_from_file(fh):

try:
# If possible, read the file as JSON
json_data = json.load(fh)
return json.loads(fh)
except:
# If not, read the file as a string, and try to parse it as JSON
contents = ""
for line in fh:
for line in fh.splitlines():
cleanedLine = line.split("//", 1)[0]
if len(cleanedLine) > 0 and line.endswith("\n") and "\n" not in cleanedLine:
cleanedLine += "\n"
Expand All @@ -20,9 +20,9 @@ def get_json_from_file(fh):
return json.loads(contents)

def main():
for file in glob.glob("**/*.json"):
for file in glob.glob("**/*.json", recursive=True):
with open(file, "r") as fh:
json_data = get_json_from_file(fh)
json_data = get_json_from_file(fh.read())

with open(file, "w") as fh:
json.dump(json_data, fh, indent=2)
Expand Down