I have a large JSON file. My goal is to minify in using Python.
The JSON file contains Arabic and Bengali Characters.
My problem is when I try to minify is I am getting Unicode characters instead of normal string characters like this \u09c7
.
How can I save the minified file with normal string characters?
Below is my code:
import json
filename = 'quran.json' # file name we want to compress
newname = filename.replace('.json', '.min.json') # Output file name
fp = open(filename, encoding="utf8")
print("Compressing file: " filename)
print('Compressing...')
jload = json.load(fp)
newfile = json.dumps(jload, indent=None, separators=(',', ':'))
newfile = str.encode(newfile)
f = open(newname, 'wb')
f.write(newfile)
f.close()
print('Compression complete!)
Here is the file link in case you want to try: https://raw.githubusercontent.com/nhridoy/quran-api/main/v1/quran.json
CodePudding user response:
it need ensure_ascii=False
flag in json.dumps()
import json
filename = 'quran.json' # file name we want to compress
newname = filename.replace('.json', '.min.json') # Output file name
with open(filename, encoding="utf8") as fp:
print("Compressing file: " filename)
print('Compressing...')
jload = json.load(fp)
newfile = json.dumps(jload, indent=None, separators=(',', ':'), ensure_ascii=False)
#newfile = str.encode(newfile) # remove this
with open(newname, 'w', encoding="utf8") as f: # add encoding="utf8"
f.write(newfile)
print('Compression complete!')