Skip to content

Instantly share code, notes, and snippets.

@pkliang
Created December 26, 2023 17:40
Show Gist options
  • Save pkliang/fca8d7cd64eac1f27a78b250b772987f to your computer and use it in GitHub Desktop.
Save pkliang/fca8d7cd64eac1f27a78b250b772987f to your computer and use it in GitHub Desktop.
Format json file without whitespaces
import json
import glob
def format_json_compat(file):
json_file = open(file)
raw_json = json.load(json_file)
json_compat = json.dumps(raw_json, indent=None, separators=(',', ':'))
print(json_compat)
write_file = open(file, 'w')
write_file.write(json_compat)
# 把json文件和此脚本放在同一目录下,运行此脚本即可
for filename in glob.glob('*.json'):
format_json_compat(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment